Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Click this OR that, then do something

I suppose the title is quite self-explanatory.

I want to close a div when a user clicks on an overlay OR on a link. I know you can just write two functions like so:

$("#close-search").click(function() {
    $("#branding #searchform").fadeOut("fast");
    $("#global-overlay").fadeOut("fast");
});

$("#global-overlay").click(function() {
    $(this).fadeOut("fast");
    $("#branding #searchform").fadeOut("fast");
});

Or you can write one function, like so:

function closeSearch {
    $(this).fadeOut("fast");
    $("#branding #searchform").fadeOut("fast");
}

$("#close-search").click(function() {
    closeSearch();
});

$("#global-overlay").click(function() {
    closeSearch();
});

I tried this, but it didn't work.

$("#close-search", "#global-overlay").click(function() {
        $("#branding #searchform").fadeOut("fast");
        $("#global-overlay").fadeOut("fast");
    });

But is it possible to write this in one line? (Something like $("#close-search" OR #global-overlay"))

like image 809
Bram Vanroy Avatar asked Jun 22 '12 12:06

Bram Vanroy


2 Answers

comma seperated means this or that

$("#close-search, #global-overlay")

Multiple selector

like image 168
AbstractChaos Avatar answered Nov 10 '22 17:11

AbstractChaos


Try this

$("#close-search, #global-overlay").click(function() {
      $("#branding #searchform").fadeOut("fast");
      $("#global-overlay").fadeOut("fast");
});
like image 4
Dale Avatar answered Nov 10 '22 17:11

Dale