galvin Posted March 31, 2010 Share Posted March 31, 2010 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 https://forums.phpfreaks.com/topic/197051-get-value-and-text-from-an-anchor-tag/ Share on other sites More sharing options...
xenophobia Posted March 31, 2010 Share Posted March 31, 2010 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 https://forums.phpfreaks.com/topic/197051-get-value-and-text-from-an-anchor-tag/#findComment-1034469 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.