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? Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.