Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
39
rated 0 times [  40] [ 1]  / answers: 1 / hits: 104247  / 11 Years ago, tue, may 28, 2013, 12:00:00

So I start with items 1-4:



<div class=someDiv bold italic style=display: none;>Lorem</div>
<div class=someDiv regular italic style=display: block;>Lorem</div>
<div class=someDiv bold style=display: none;>Ipsum</div>
<div class=someDiv regular style=display: block;>Ipsum</div>


Then I have some input checkboxes:



<input class=regular type=checkbox />
<input class=bold type=checkbox />
<input class=italic type=checkbox />


So basically I have jQuery showing and hiding divs. Now I have another function that must iterate through these divs (one for each checkbox), and show/hide based on another criteria. But I don't want the already hidden divs to be shown again.



$(.someDiv).each(function(){
if($(this).hasClass(regular)){
$(this).show();
} else {
$(this).hide();
};


In this example, the only remaining div should be the last div. Unfortunately, this code will make the second and fourth divs shown.



This code is very basic example of all the filters I'm going to be applying, adding etc.


More From » jquery

 Answers
12

You can use the :visible selector to find only visible.



$(.someDiv:visible).each(....);


You can use the .not() selector to find only hidden.



$(.someDiv).not(:visible).each(....);


I think you can perform the same operation in your code with this one line.



$(.someDiv).hide().find(.regular).show();


Find all .someDiv and hide them, then find those with a .regular class and show them.


[#77985] Sunday, May 26, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
quentinaveryb

Total Points: 102
Total Questions: 100
Total Answers: 93

Location: Colombia
Member since Mon, May 2, 2022
2 Years ago
quentinaveryb questions
Thu, Aug 6, 20, 00:00, 4 Years ago
Fri, Jul 17, 20, 00:00, 4 Years ago
Mon, Aug 12, 19, 00:00, 5 Years ago
;