views:

291

answers:

5

Is it possible to apply a jQUery method such as .hide() to many divs with different id parameters in one go.

The example for a single div is:

$("#action-div").click( function() { $("#some-div-to-hide").hide("fast"); } );

Can this be done for many divs with different ids, without having to handle the .click event many times.

+3  A: 

You probably want to use a class attribute on the divs you want to operate on, then specify the class in the selector.

<div class="actiondiv" ...>

and then

$(".actiondiv").click(...)
Vinay Sajip
+1  A: 

The simplest method is give all the ids the same class and later:

$(".some_class").click( function(){.....});
astropanic
+3  A: 

Comma seperate the list of ID's.

$("#action-div, #anotherId, #yetAnotherId").click(function(){

});
Fermin
Didn't work for me, until I removed the spaces after the comma.
Steven
+1  A: 

You could always apply a single class to these items, and select it based off the class:

$(".action-class").click( function() { $("#some-div-to-hide").hide("fast"); } );
Pete OHanlon
+1  A: 

If you want to have the click event attached to all divs, use a common class (<div class="clickhidden">):

$(".clickhidden").click( function() { $("#some-div-to-hide").hide("fast"); } );

If you want one click to hide multiple classes, attach the class to the target:

$("#action-div").click( function() { $(".clickhidden").hide("fast"); } );

Combinations are also possible.

Scharrels