Could you help me to understand – where I made the mistake. I have the following html code:
<div id="container">
<a href="#info-mail.ru" id="getInfo" onClick="return false;">Info mail.ru</a>
</div>
<div id="container">
<a href="#info-mail.com" id="getInfo" onClick="return false;">Info mail.com</a>
</div>
<div id="container">
<a href="#info-mail.net" id="getInfo" onClick="return false;">Info mail.net</a>
</div>
and the following js code (using jQuery):
$('#getInfo').click(function(){
alert('test!');
});
“Click” event fired only on first link element. But not on others.
I know that each ID in html page should be used only one time (but CLASS can be used a lot of times) – but it only should (not must) as I know. Is it the root of my problem?
TIA!
upd: Big thx to all for explanation!:)
Use a class for this (and
return falsein your handler, not inline):http://jsfiddle.net/Xde7K/2/
The reason you’re having this problem is that elements are retrieved by
IDusingdocument.getElementById(), which can only return one element. So you only get one, whichever the browser decides to give you.