I have these divs, each have the same class .onediv and distinct ids div1, div2, div3
<div class="alldivs">
<div class="onediv" id="div1">
</div>
<div class="onediv" id="div2">
</div>
<div class="onediv" id="div3">
</div>
</div>
I want to use jquery to change their css display property so that all of them are display none except id=div2 which is display block. Right now I’m doing it this way with 2 lines. Is there a quicker way to do the same thing with one line only?
$('.onediv').css('display', 'none');
$('#div2').css('display', 'block');
If you’re hiding/showing amongst siblings then the most generic (and still performant) solution is to use
.siblings(), like this:Note that
.show()and.hide()are shortcuts for setting thedisplayCSS property like you want here (betweennoneand what it was before,blockin the default<div>case).