Figure out div that is visible out of four divs
-
09-06-2019 - |
Question
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?
Solution
Assign the same class to each div then:
$("div.myClass:visible").attr("id");
OTHER TIPS
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow