How to get hyperlink text using javascript

I have hyperlink control and button control as follows:

Collapse | Copy code

How can I get hyperlink text using javascript:

+6
source share
5 answers

Do you mean:

var hyperlinkText = document.getElementById('yourAnchorId').innerHTML; 
+9
source

You can do it.

 var test=document.getElementById(your link id).text; 

// test will return the text of your hyperlink

+5
source

If you use jQuery, the easiest way is to get the id of this specific link, for example

 <a href="http://www.stackoverflow.com" id="testlink">Click here to go to stack overflow</a> <script type="text/javascript"> $(document).ready(function(){ var linktext=$('#testlink').text(); }); </script> 

or if you are not using jquery

 <script type="text/javascript"> var linktext=document.getElementById('testlink').text; </script> 

If you do not use jquery, do not forget to put javascript after the html anchor is declared, otherwise the code will not work, since the anchor will not exist if the script is running, if javascript matches before the html anchor.

+2
source
 var link = document.getElementById("linkid"), text = link.innerHTML; 
0
source

try it

document.getElementById('myAnchor').innerHTML;

0
source

Source: https://habr.com/ru/post/910343/


All Articles