I have a table with some data inside. When I click on a element, it adds a class .active to :
$('#someId').on('click', 'tr', function (e) { var target = $(this).find(".target"); var prev = target.html(); timeCell.html(target.data('prev')); timeCell.data('prev', prev); if ($(this).hasClass('active')) { $(this).removeClass('active'); } else { $(this).addClass('active'); } }); As you can see after clicking on tr, users could see some icons instead of previous data (.target) and I want them to click on icons ("prev") that call some other functions. But when the icons are clicked, a click on a parent element is execute too. How can I avoid this?
---- If anybody needs help in this case - here's the solution!
$('#someId').on('click', 'tr', function (e) { var senderName = e.target.tagName.toLowerCase(); if (senderName === 'div') { //do some staff } else { var target = $(this).find(".target"); var prev = target.html(); timeCell.html(target.data('prev')); timeCell.data('prev', prev); if ($(this).hasClass('active')) { $(this).removeClass('active'); } else { $(this).addClass('active'); } });
e.stopPropagation()will stop the click event from bubbling up to the parent<tr>element ;)