Search code examples
javascriptjquerycss-selectorsvisible

How to check visibility of multiple div's in simpler way?


Currently i'm using below code which works well.

$("#topperAtBaseLevel:visible, #lowerAtBaseLevel:visible, #midAtBaseLevel").hide();

any optimised code? (i cant use same class) i mean how to use :visible rightly?


Solution

  • That is the way to achieve what you're going for. You are using the selectors correctly and efficiently.

    You could make it a little faster if you maintained an array of the ID's of the tags that need to be hidden, and then construct your selector dynamically to find by ID. (This would be faster even though the selector might be longer. Selecting by ID is very fast.)

    But, the optimization is not needed, is it? We're talking about going from lightening fast to double lightening fast. A super-duper jQuery pro would just do what you've done.