views:

4472

answers:

2

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?

+7  A: 

Assign the same class to each div then:

$("div.myClass:visible").attr("id");
Gilean
Of course it's the most obvious answer that I overlook.
RedWolves
A: 

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.

Jim