given this HTML,
<div id="dd_container" class="dd dd_container">
<ul class="dd_deploy">
<li class="dd_deploy">
more options
</li>
</ul>
<ul class="dd">
<li class="dd">
el1
</li>
<li class="dd">
el2
</li>
<li class="dd">
el3
</li>
</ul>
</div>
why does this jquery function print to the console two times?
$(document).on('click','.dd_deploy', function(){
console.log($(this).parents('.dd_container').children('ul.dd'));
});
Because you have 2
.dd_deployelements, one nested inside the other so the event is bubbling.If this HTML structure is intended, you can use
event.stopPropagationto prevent the event bubbling, like this:Note also that having
$(document)as the primary selector on a delegated event handler is not the ideal for performance. Instead ofdocumentuse the nearest element which is not appended to the DOM dynamically.