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:
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.
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.
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.
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.
you can get like this.
var test=document.getElementById(your link id).text;
//test will return the text of your hyperlink
Do you mean:
var hyperlinkText = document.getElementById('yourAnchorId').innerHTML;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With