JavaScript - - By Sam Deering

Attaching Events to Elements of the Same ID

OK so we’ve all ran into this problem for some reason or another – 2 elements with the same id.

Basically, you shouldn’t have elements on the same page with the same id, it’s bad programming! If however, you do find yourself in this situation (ie. your trying to fix or extend someone else’s code). You can add different class names to the elements and use the $(‘.elementclass’).attr($(this).src) command to determine which element you have chosen.

If you want to reference elements together then you can simply add them to the same class and select them this way (using a full stop instead of a hash).

2 elements with same id example


$('.elementclass').attr($(this).src);
Sponsors
Login or Create Account to Comment
Login Create Account