javascript - hide div if it doesn't have a certain class -
i need hide class if doesn't have class active
.
$(document).ready(function() { if (!$('.active').hasclass('active')) { $(this).hide(); }; });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="subheader active"> <p>hello</p> </div> <div class="subheader"> <p>goodbye</p> </div> <div class="subheader"> <p>hello again</p> </div>
to hide subheader
doesn't have class active
, use
$('.subheader:not(.active)').hide();
a better solution be, think, use pure css this:
.subheader { display: none; } .subheader.active { display: block; }
as demonstrated in snippet, uses jquery toggle class, , uses css decide happens visually.
$('.subheader').on('click', function(){ $('.subheader').toggleclass('active'); });
.subheader { display: none; } .subheader.active { display: block; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="subheader active"> <p>hello, click toggle</p> </div> <div class="subheader"> <p>goodbye</p> </div> <div class="subheader"> <p>hello again</p> </div>
Comments
Post a Comment