Anne Schwarz Anne Schwarz - 11 days ago 5
HTML Question

jQuery click on anything but div AND EVERYTHING INSIDE

I use tis code:

$(document).bind('click', function(e) {
if(!$(e.target).is('#paybox')) {
$('#paybox').hide();
}
});


to hide
#paybox
when user click anywhere but
#paybox
.

But when I click on "radio" form:

<div id="paybox">
<table width="100%">
<tr>
<form method="post" action="">
<td>
<input type="radio" name="dauer" value="small" checked>
</td>

<td>
<input type="radio" name="dauer" value="mid">
</td>

<td>
<input type="radio" name="dauer" value="big">
</td>
</form>
</tr>
</table>
</div>


INSIDE the
#payboy
then
#payboy
get hidden!

How to prevent this bulls...?

Answer

You should check whether or not your click is inside paybox or not. Something like this should work:

$('body').on('click', function(e) {
  if(!$(e.target).is('#paybox') && $(e.target).parents('#paybox').length === 0) {
    $('#paybox').hide();
  }
});

So this only hides the #paybox element if the click is neither on paybox nor any node that is within the #paybox element.