views:

39

answers:

2

I have a div used for filtering and i want it to close when anything outside of the div is clicked. The selector I am trying to build, basically selects all elements except a specific element and excludes its children. Here is what ive tried, but havent been able to get working

$('*:not(:has(.exclude *))').live('click', function() {HideFilter();});

page structure is simplified to this:

<div></div>
<div>
 <div></div>
 <div>
  <div class="exclude"><inputs></div>
 </div>
 <div></div>
</div>

so i want all of the divs, but the one and everything in the .exclude to have the event. Ive been at this for a while, i need some help.

A: 

how about a slightly different strategy:

close everything when anything but the div is clicked:

$("body").click(function(e){
  if($(e.target).hasClass("exclude")){
    //show stuff
  } else {
    //hide stuff
  }
});

something like that

mkoryak
This will help if i cannot get my selector working. thanks.
+1  A: 

Have you tried

$('*:not(.exclude, .exclude *)').live( ... )

? [edit[ Ah I see - the problem is that even if you exclude the stuff, the events still bubble up.

Try something like this:

$('*').live('click', function(e) {
  if (!$(e.target).is('.exclude, .exclude *'))
    // do interesting stuff
  }
  return false;
});

That should stop event propagation on the excluded things without actually doing anything.

Example page: http://gutfullofbeer.net/balloon.html

Pointy
I have tried this and it still puts the events on all of the chlidren of .exclude
Are you sure that you typed it exactly like that?
Pointy
yes. debugging my code i notice the selector is working correctly. Its only applying the event to the correct elements. But the elements under my div must be picking up the click also.
This solved my problem. thanks.