Jump to content

get value and text from an anchor tag?


galvin

Recommended Posts

If I have this anchor in my HTML...

 

<a class="team" name="pick1" onclick="win(this)" value="2">Team 2</a>

 

...1. how do I get the VALUE (in this case "2") using Javascript?

 

2.  how do I get the text "Team 2"?

 

I know getElementById but my anchor tags don't have IDs and I don't want to give them any because I know there is another way.  Can anyone help?

 

 

 

Link to comment
Share on other sites

There are many ways.. From your code, are you going to get those value in your 'win' function?

If that is the case, it's easy:

function win(anchor) {
    var value = anchor.getAttribute('value');  // This give you '2'
    var html = anchor.innerHTML;  // This give you 'Team 2'
}

But if you want to access from somewhere else without using ID, you must get the reference using it's name:

var anchors = document.getElementsByName('pick1');
var myAnchor = anchors[0];
var value = myAnchor .getAttribute('value');  // This give you '2'
var html = myAnchor .innerHTML;  // This give you 'Team 2'

 

Hope this help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.