Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Figure out div that is visible out of four divs

I need to figure out what div is visible out of four possible divs using jQuery. Only one of those div's will be visible at any given time.

This is what I have that works so far:

$("#FeatureImage1:visible, #FeatureImage2:visible, #FeatureImage3:visible, #FeatureImage4:visible").attr("id");

Is there a way to refactor this? Is there an easier way to figure this out?

like image 437
RedWolves Avatar asked Sep 14 '08 17:09

RedWolves


2 Answers

When applicable, it's better to use contextual selectors rather than add spurious classes. For instance, if the <div> elements are the only children of an element with id="foo", then using $("#foo > div:visible").attr("id") would better reflect the purpose of the code.

like image 30
Jim Avatar answered Sep 30 '22 16:09

Jim


Assign the same class to each div then:

$("div.myClass:visible").attr("id");
like image 112
Gilean Avatar answered Sep 30 '22 14:09

Gilean