Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the text of the hyperlink using javascript

Tags:

javascript

i am having a hyperlink control and a button control like this:

Collapse | Copy Code

How can i get the text of the hyperlink using javascript:

like image 995
user1259223 Avatar asked Mar 09 '12 11:03

user1259223


People also ask

How do I get text in JavaScript?

Use the textContent property to get the text of an html element, e.g. const text = box. textContent . The textContent property returns the text content of the element and its descendants.

How do I get the link in JavaScript?

Answer: Use the window. location. href Property location. href property to get the entire URL of the current page which includes host name, query string, fragment identifier, etc. The following example will display the current url of the page on click of the button.

How do you find a link in a string?

Use a Regex to Find URLs in a String In the function, we refine the urlRegex variable that has the regex for matching URLs. We check for http or https . And we look for slashes and text after that. The g flag at the end of the regex lets us search for all URLs in the string.

How do I change the text of a link using JavaScript?

Use the textContent property to change the text of a link element, e.g. link. textContent = 'Replacement link text' . The textContent property will set the text of the link to the provided string, replacing any of the existing content.


Video Answer


3 Answers

you can get like this.

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

//test will return the text of your hyperlink

like image 126
Mulesoft Developer Avatar answered Oct 16 '22 15:10

Mulesoft Developer


Do you mean:


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

like image 39
Sudhir Bastakoti Avatar answered Oct 16 '22 16:10

Sudhir Bastakoti


If you are using JQuery The easiest method is to get via id of that particular link such as

<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 don't use jquery

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

If you are not using jquery remember to put javascript after the html declaration of anchor otherwise code won't work as anchor would not exist when script is running if javascript comes before anchor html.

like image 39
Roshan Budhathoki Avatar answered Oct 16 '22 17:10

Roshan Budhathoki