2

does anyone know if css position:relative; can mess up the function

    $('body').not($('.theDIV')).click(function(){
    alert('');

    });

or the problem is somewhere else?

what is happening is i have a that appears on click of a button and i want it to hide() when i click anywhere on the body, except the div itself. HTML

<ul class='messages'> //these are made dynamically. should i use each() to go through all the elements?
<li>
    <div class='theDIV'></div>
    <input type='button'>
</li>
<li>
    <div class='theDIV'></div>
    <input type='button'>
</li>
<li>
    <div class='theDIV'></div>
    <input type='button'>
</li>
</ul>

sorry if i wasnt clear the first time

Semur Nabiev
  • 792
  • 5
  • 14
  • 27

1 Answers1

6

You could do

$('body').click(function(e){
   if(! $(e.target).hasClass('theDIV')){
     alert('clicked on something that has not the class theDIV');
   }

});
Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192