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?
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.
Assign the same class to each div then:
$("div.myClass:visible").attr("id");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With