Reputation: 1272
I'm having a problem with my .fadeOut()
not working when a user clicks "No", and I know it's because div.confirm
is embedded within the span
, which is where the .fadeIn()
function is called.
I'm working on a project that requires this HTML hierarchy, but I need the "No" button to .fadeOut()
the confirmation box.
My HTML:
<span>Delete File
<div class="confirm">Are you sure?
<div class="yes">
Yes
</div>
<div class="no">
No
</div>
</div>
</span>
JS:
$('span').click(function () {
$('.confirm').fadeIn(100);
});
$('.no').click(function () {
$('.confirm').fadeOut(100);
})
Upvotes: 2
Views: 3222
Reputation: 24001
you need to use e.stopPropagation();
$('span').click(function () {
$('.confirm').fadeIn(100);
});
$('.no').click(function (e) {
e.stopPropagation();
$('.confirm').fadeOut(100);
});
And , to be more clear if you have more than one span you can use
$('span').click(function () {
$(this).find('.confirm').fadeIn(100);
});
$('.no').click(function (e) {
e.stopPropagation();
$(this).closest('.confirm').fadeOut(100);
})
Upvotes: 5
Reputation: 46
A simple solution is moving the .confirm div outside the span - it should prevent the firing of the span's event and still work just fine.
<span>Delete File</span>
<div class="confirm">Are you sure?
<div class="yes">
Yes
</div>
<div class="no">
No
</div>
</div>
Upvotes: 3