I have a parent div named #container
and have many children which have a common class of .timescalebase
. My requirement is when I click in my parent div
I want the nearest child. I can't use the child id for click event because its width is 0px
.
<div id="container" style="width: 70%; margin-top: 15px; position: absolute;">
<div class="timescalebase" id="1"></div>
<div class="timescalebase" id="2"></div>
<div class="timescalebase" id="3"></div>
</div>
$(document).on('click', '#container', function (e) {
base = $(this).closest(".timescalebase")
baseid = base.attr('id');
});
click
event handler directly on the#container .timescalebase
elements and usethis
within the handler to refer to the element?id=1;
is invalid HTML.