Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery $('html, body').not()

Tags:

jquery

click

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>​
like image 634
Adarchy Avatar asked Oct 18 '12 15:10

Adarchy


People also ask

What is not() in jQuery?

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.

How do I get HTML using jQuery?

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.

What does HTML() do?

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.


1 Answers

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');
});​
like image 68
wirey00 Avatar answered Sep 30 '22 17:09

wirey00