Click on it OR, then do something

I believe the title is pretty clear.

I want to close the div when the user clicks on the overlay OR by the link. I know that you can simply write two functions:

$("#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, for example:

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

I tried this, but it did not work.

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

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

+8
jquery selector
source share
3 answers

separated comma means one or the other

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

Multiple selector

+21
source share

try it

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

Unverified:

Try the following:

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

Note that the string in the selector is not split like an array; instead, the Sizzle selector will do this for you.

0
source share

All Articles