views:

1929

answers:

5

Hi please look at the HTML below. I am trying to use jQuery to get every 3rd instance on the DIVs with class="box" contained within the DIV with class="entry" to have a no right hand margin:

My HTML code:

<div class="entry">

    <div class="box">
        SOME HTML....
    </div><!-- end .box -->

    <div class="box">
        SOME HTML....
    </div><!-- end .box -->

    <div class="box">
        SOME HTML....
    </div><!-- end .box I Want to remove right hand margin on this div -->

    <div class="box">
        SOME HTML....
    </div><!-- end .box -->

    <div class="box">
        SOME HTML....
    </div><!-- end .box -->

    <div class="box">
        SOME HTML....
    </div><!-- end .box I Want to remove right hand margin on this div -->

    <div class="box">
        SOME HTML....
    </div><!-- end .box -->

    <div class="box">
        SOME HTML....
    </div><!-- end .box -->

    <div class="box">
        SOME HTML....
    </div><!-- end .box I Want to remove right hand margin on this div -->

    </div>
    <!--end entry-->

My attempt with jQuery:

   <script>
        $(document).ready(function(){
            $("div.entry:nth-child(3)").css("margin", "0px");
        });
   </script>

I can't get this working can anyone please help? Thanks in advance!

+2  A: 

Your :nth-child selector does not reference n, and you need to reference the inner div in your selector.

Try:

$(document).ready(function(){
  $("div.entry div:nth-child(3n)").css("margin", "0px");
});
Dominic Rodger
Tried that thanks but no joy any other ideas?
mtwallet
Sorry - just spotted that your selector was wrong too. You still need the `3n` rather than just `3`, but you also need to put ` div` before the `nth-child` stuff. I've tested this and it works.
Dominic Rodger
Damn, now you’ve corrected it. ;-)
Gumbo
I still can't get this working using the solution you provided. I have the div.entry nested inside a couple of other div's could this be the reason why? Do I have to reference the entire path i.e "div1 > div2 > div.entry div:nthchild(3n)"?
mtwallet
Whether it's nested insid eother divs should make no difference, you certainly don't have to reference the entire path. Is there anything else interfering with those innermost divs? Are there any other divs inside entry that you don't want to be affected? Perhaps you want `$("div.entry div.box:nth-child(3n)").css("margin", "0px");`
Dominic Rodger
FWIW, I've tested my solution with the `entry` div nested inside other and it works fine.
Dominic Rodger
@Dominic: Thanks again for the help. I've just tried running this simple line $("div.entry").addClass("test"); and thats not working I must have something conflicting but I've no idea what as I have stripped out all other JQuery (apart from the library itself).
mtwallet
A: 

From the docs (my emphasis)

Matches all elements that are the nth-child of their parent or that are the parent's even or odd children.

You're currently selecting the parent, while you should be selecting children:

$("div.entry > div:nth-child(3)").css("margin", "0px");
David Hedlund
Thanks for your help much appreciated unfortunately that did not work.
mtwallet
+1  A: 

Try this selector:

div.entry > div.box:nth-child(3n)
Gumbo
Again thanks for the help but it didn't work
mtwallet
@mtwallet: Well, it works for me.
Gumbo
A: 

Hi, thanks to all who helped the solution provided is indeed correct. I am coding up a supplied template and found that JQuery had been set to run in compatibility mode hence $ was the problem.

mtwallet
+1  A: 

nth-child also seems to be non-0 indexed. Keep that in mind if you're used to indexing at 0.

Boris Lutskovsky