If you're looking for a way to efficiently handle element selection and display in JavaScript, one approach is to dynamically generate a selector from an array. This allows you to show all the matching elements while hiding the rest by default:
JavaScript
const classes = ['foo', 'bar', 'baz']
,selector = classes.map(x => '.' + x).join('')
$(selector).show()
CSS
div {
display: none;
}
Take a look at the JS Fiddle demo for more information.
Alternatively, if you're working with a large number of elements, it may be more efficient to generate a custom style sheet in JavaScript:
const classes = ['foo', 'bar', 'baz']
,selector = classes.map(x => '.' + x).join('')
,$style = $(`<style>div {display: none;} div${selector} {display: block;}</style>`)
$('head').append($style)
To undo this styling, simply remove the <style>
element by using $style.remove()
.
Check out the JS Fiddle demo to see this in action.