Adjit
Adjit

Reputation: 10305

JQuery Conditional Events

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!

Upvotes: 2

Views: 1818

Answers (1)

adeneo
adeneo

Reputation: 318352

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.

Upvotes: 9

Related Questions