here - I want alert to happen when you click anywhere but not on the div.
When I click on div, alerts shows too.
JS
$("html, body").not('.entry-content').click(function() {
alert('d');
});
HTML
<div class="entry-content"> kkkk </div>
The not() is an inbuilt function in jQuery which is just opposite to the filter() method. This function will return all the element which is not matched with the selected element with the particular “id” or “class”. The selector is the selected element which is not to be selected.
To get HTML content of an element using jQuery, use the html() method. The html() method gets the html contents of the first matched element.
Definition and Usage The html() method sets or returns the content (innerHTML) of the selected elements. When this method is used to return content, it returns the content of the FIRST matched element. When this method is used to set content, it overwrites the content of ALL matched elements.
You can use the event argument to see what target was clicked and return false
$("html, body").click(function(e) {
if ($(e.target).hasClass('entry-content')) {
return false;
}
alert('d');
});
http://jsfiddle.net/keyZw/
You are using the .not() filter.. but it's still part of your html/body.. so you need to handle it inside the click function. Also you are just binding the click event..
So
// find html,body - not ones with class=entry-content - bind click
$("html, body").not('.entry-content')
So that doesn't prevent the alert as your div is still inside the body
As mentioned.. you only need to bind to the body really
$("body").click(function(e) {
if ($(e.target).hasClass('entry-content')) {
return false;
}
alert('d');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With