Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery stop "click" action after first elevent

Tags:

jquery

There are two nested elements, both have different click actions.
I need to stop outer element action when inner element is clicked.

HTML:

<div id='out'>
    <div id='in'></div>
</div>

jQuery:

$('#out').click(function(){alert('OUT div is pressed')})
$('#in').click(function(){alert('IN div is pressed')})

I need when in is pressed, only his action is executed. out's script should have no action.

How it can be solved?

like image 318
Qiao Avatar asked Jan 19 '23 16:01

Qiao


1 Answers

You should use stopPropagation():

$('#in').click(function(e){
   alert('IN div is pressed')
   e.stopPropagation();
});
like image 84
alexn Avatar answered Mar 02 '23 20:03

alexn