I’m using the on handler to register to the register to the ‘body’ element and fire an event whenever an element which is not in a specific div.
Therefor I’m using the not selector to filter out all those elements. However if I click the element, the event is fired twice, instead of once.
Am I misunderstanding the not selector?
Fiddle sample: http://fiddle.jshell.net/kHbH9/
Sample code
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Demo</title>
<script src="jquery.js"></script>
<script>
$(function() {
$('body').on('click', ':not(#toolbar)', function(){
$('body').append('clicked<br/>')
});
});
</script>
</head>
<body>
<div id"toolbar">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</div>
<h1>Click here</h1>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</body>
</html>
The culprit is
id"toolbar"which should beid="toolbar". You can usestopPrapagationmethod which prevents the event from bubbling.http://fiddle.jshell.net/PHkbR/