1

I need to remove all classes of 'no-right-marg' on all #tips li elements. I then need to hide all those elements with a class of the clicked element's ID. Then, I need to check which elements are still being displayed and add a class of 'no-right-marg' to every fourth one. My code below isn't working. Please see my jsfiddle http://jsfiddle.net/VCZc4/2/

jQuery('#selector li').click(function() {

   colour = '.' + jQuery(this).attr('id');
   jQuery('#tips ' + colour).toggle();
   jQuery(this).toggleClass('inactive');
   jQuery('#tips li').removeClass('no-right-marg');

   jQuery('#tips li:visible').each(function(index){

       if(index %4===0 ){// if divisible by 4
           jQuery(this).addClass('.no-right-marg');

       }            
   });

});​

HTML

<ul id="selector">
    <li id="brown">button 1</a>
    <li id="green">button 2</a>
    <li id="blue">button 3</a>
    <li id="orange">button 4</a>

</ul>

<ul id="tips">
    <li class="brown">text</li>
    <li class="orange">text</li>
    <li class="blue">text</li>
    <li class="blue no-right-marg">text</li>
     <li class="blue">text</li>
     <li class="orange">text</li>
     <li class="blue">text</li>
    <li class="blue no-right-marg">text</li>
    <li class="green">text</li>
</ul>
​

2 Answers 2

6

There seems to be a "." in your addClass call. Removing the "." fixes your code.

http://jsfiddle.net/reygonzales/VCZc4/5/

I hope that gets what you want.

Sign up to request clarification or add additional context in comments.

5 Comments

thanks for picking that up, it does do something but not the right thing! It adds the class, but not to every 4th element...
Ok, so check my new fiddle: jsfiddle.net/reygonzales/VCZc4/10 Like Sir Jonathan Rowny says, it's zero-indexed. So I added 1 to the index before applying the modulo operator. How about now?
ahh so thats what zero indexed means, that it starts at 0.
Oh muh gawd! My first accepted answer! Now I can finally comment on questions to clarify what the user is asking
This may be the first time I've ever been referred to as "Sir Jonathan Rowny" Ha.
1

It's zero-indexed, therefore mod 4 == 3 is what you need. if(index % 4 === 0 ) should be if(index % 4 === 3 )

2 Comments

thanks, I've done that but it's still not adding it to every 4th element
bah, my bad, I meant 3! index should be 3. Here's the working fiddle: jsfiddle.net/VCZc4/11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.