I have different divs
that have an id="item"
. When I select one of the divs
, a new class should be added to a
where the div has been selected. Is there any way to do this when the IDs are not unique?
HTML:
<div class="something" id="item">
<a href="#" class="something">...</a>
</div>
<div class="something" id="item">
<a href="#" class="something">...</a>
</div>
<div class="something" id="item">
<a href="#" class="something">...</a>
</div>
<div class="something" id="item">
<a href="#" class="something">...</a>
</div>
JavaScript (what I have tried, but does not work):
// changes all links on the website
$("body").delegate('#item', 'click', function() {
$( "a" ).addClass( "active" );
});
Edit: I am using the delegate
function because I create all the divs with PHP, otherwise the click event is not registered.