I have written this code. Its not working but with this you should get idea what i am trying to achieve
$('.jfmfs-alpha-separator').filter(function() {
return ($(this).next().not(".hide-filtered").hasClass("jfmfs-alpha-separator"));
}).addClass('hide-filtered');
I have number of divs at same level. Below div with class jfmfs-alpha-separator i have number of divs and then div jfmfs-alpha-separator and again number of divs (note with below i mean at same level not child). now when all divs that follow jfmfs-alpha-separator gets class .hide-filtered I want to hide that jfmfs-alpha-separator div too.
In my code i am trying to find next div after div with class jfmfs-alpha-separator which does not have ‘hide-filtered’ class and has jfmfs-alpha-separator class. if this check retutns true i hide that div.
Example:
<div class ="jfmfs-alpha-separator">Letter A</div>
<div class ="friend hide-filtered">Aaron</div>
<div class ="friend hide-filtered">Aaron</div>
<div class ="friend hide-filtered">Ashutosh</div>
<div class ="jfmfs-alpha-separator">Letter B</div>
<div class ="friend hide-filtered">Bob</div>
<div class ="friend">Baron</div>
<div class ="friend hide-filtered">Batista</div>
<div class ="jfmfs-alpha-separator">Letter C</div>
<div class ="friend hide-filtered">Carl</div>
<div class ="friend hide-filtered">Chris</div>
<div class ="friend hide-filtered">Charlie</div>
<div class ="jfmfs-alpha-separator">Letter D</div>
...
In above example letter A and C should get hidden because all names below it has got hide-filtered class and Letter B should remain as it is.
How do I achieve this?
Final Solution is used using Salmans Answer :
$(".jfmfs-alpha-separator").each(function () {
if ($(this).nextUntil(".jfmfs-alpha-separator").not('.hide-filtered').length == 0) {
$(this).addClass('hide-filtered');
}else{
$(this).removeClass('hide-filtered');
}
});
Here is one way to write your code:
It is easy to convert this logic to
.filter(function() {})