user1936192
user1936192

Reputation: 345

jquery slideToggle on all open div s

i have div s with id like that: manufacturer_12, manufacturer_37, manufacturer_71, etc...

is there a way to find all the div s which their visibility is visible, and to do to them something like:

$('[id^="manufacturer"]').slideToggle("slow");

the problem is that its imposibble to make a for loop because their id isn't consecutive.

maybe i should use http://api.jquery.com/jQuery.each/ ?

Upvotes: 0

Views: 125

Answers (3)

Alexander
Alexander

Reputation: 23537

Indeed, you can use :visible.

$('[id^="manufacturer"]').filter(":visible").slideToggle("slow");

But be aware that .slideToggle() will apply to all matched elements as the documentation says. So, you actually don't need to use .each() here.

.slideToggle( [duration] [, callback] )

Returns: jQuery

Description: Display or hide the matched elements with a sliding motion.

In this case, all visible elements with id starting with manufacturer.

Upvotes: 2

vyakhir
vyakhir

Reputation: 1793

Believe me, that's not a good way of doing things.
You'd better assign a specific class to all items of interest and write a very simple selector basing on class name only. This will work much faster.

Upvotes: 1

Explosion Pills
Explosion Pills

Reputation: 191729

You could use .each, but this may work:

$("[id^='manufacturer']:visible").slideToggle("slow");

Upvotes: 2

Related Questions