I want to alert something when user clicked anywhere in the document except the div that has class = ‘reset_btn’. I have tried both not() and :not(selector) but they are not working.
This is my code.
$('document').not('.reset_btn').click(function(){
alert('Something');
});
I have created a demo in JSFiddle. In demo they aren’t alert anything. It should alert when I clicked any area outside the div and should not alert when I clicked in div area.
Any suggestion? Thank you very much.
$('document')is not a valid selector, since there’s nodocumenttag name. You could instead use$(document), which’ll use thedocumentelement, but that’s not needed here. Just use:not()directly:To optimize this further, you should use event delegation: