Archana Kumari Archana Kumari - 4 months ago 43
Javascript Question

Prevent the click event on the parent div

<div class="usersDiv">
<ul class="users">
<li><p>$user['id']</p></li>
<li><p>$user['name']</p></li>
<li><p>$user['mobile']</p></li>
</ul>
</div>


I have an li element inside an div element.I have written Jquery that when user click on the div a different event happened and when user click on li of div a different event will happen. But the problem is when user click on li element, both the click event on li and div are called, i also used preventDefault() method on li click, but it doesn't works. How to prevent the click on div element when i click on li element? Thanks in advance.

Answer

You should use, it will prevent the parent behavior to be executed

event.stopPropagation()

You can refer this link for more details e.stopPropgation