10

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:

1
  • Which is the buttons? Which is the hyperlink? What have you tried so far? How does your HTML markup look like? Commented Mar 9, 2012 at 11:55

5 Answers 5

13

you can get like this.

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

//test will return the text of your hyperlink

Sign up to request clarification or add additional context in comments.

1 Comment

No you can't. JavaScript is case sensitive. There's no method by the name of getelementbyid() (getElementById()).
10

Do you mean:


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

Comments

1

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.

Comments

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

Comments

0

Try this

document.getElementById('myAnchor').innerHTML;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.