Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Conditional Events

Tags:

jquery

Is there anyway to have a conditional jquery event?

i.e.

if thisBox.is(clicked) or thatBox.is(clicked) {
   //do this
}

My example pertains to this piece of code:

$('.pdfClose').click(function(){
   $('.thePDF').fadeOut("slow", function(){
        $('.pdfContainer').removeClass('pdfShown');
    });
});

So what I would like to have is

($('.pdfClose') || $('.pdfContainer')).click(function(){
   $('.thePDF').fadeOut("slow", function(){
        //$(this).hide();
        $('.pdfContainer').removeClass('pdfShown');
    });
});

Something like that, but I don't even know if it exists or what the proper syntax is.

Any help is greatly appreciated!

like image 238
Adjit Avatar asked Feb 12 '26 14:02

Adjit


1 Answers

Like this

$('.pdfClose, .pdfContainer').on('click', function(){
   var self = this;

   $('.thePDF').fadeOut("slow", function(){
        $(self).removeClass('pdfShown');
    });
});

you can use multiple selectors by separating them with a comma, and inside the event handler this would reference the current one.

like image 113
adeneo Avatar answered Feb 15 '26 17:02

adeneo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!