0

I am trying to add the class last dynamically to every second row with jQuery as below

 <div class= "news-row">
    <article class="news-container fixed-page">  blah blah</article>
    <article class="news-container fixed-page">  blah blah2</article>
    </div>

Below is the jquery

 <script type="text/javascript">
    $(document).ready(function () {
        $('article.news-container :nth-child(2n)').addClass('last');
    });
</script> 

This does not add the class. Any help will be appreciated. I want the every second article element to have "last" appended.

2
  • 1
    Have you tried debugging the selector? You have an $ object for article.news-container :nth-child(2n). Check what does it resolve to using console.log. Commented Aug 8, 2014 at 20:14
  • Use :even instead of nth-child. Commented Aug 8, 2014 at 20:15

4 Answers 4

3

do not give space after selecting element

$(document).ready(function () {
    $('article.news-container:nth-child(2n)').addClass('last');
});

jsfiddle

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

1 Comment

thanks for fixing my code your solution is the closest to my original attempt and so will give you the tick
1

Try using the :odd and :even selector...

$('article.news-container:even').addClass('last');

5 Comments

thanks your solution works as well as @Ashoks recommendation using the nth and odd selector which is faster?
nth-child is much faster. But it really shouldn't be much of a concern. Use whichever one makes the most sense to you.
@ColinDeClue Can you provide a reference / tests? I tend to use the most explicit operator simply for maintainability but if there's a clear performance benefit, 2n is passably self-explanatory.
@Diin No worries... Pick whichever suits you best, that's what this site is all about! Anyway, glad to help and see you around.
@Basic: I put a link in my first comment. It's much faster, but you should know there's no reason to worry about speed in this case. I assume you're not going to be doing this a lot of times in a loop, and the speed difference is so small for a single usage that it'd be impossible to notice.
1

An article node isn't a child of itself, so that selector won't work. Start with the parent instead (whose children are the <article> nodes):

$('.news-row :nth-child(2n)').addClass('last');

jsFiddle Demo

1 Comment

there are two other elements inheriting the parent Thanks
0

You are forgetting a double quote on the first line, also do not add space before selector (:nth-child)

CodePen link below

http://codepen.io/kkirsche/pen/Hkutd

HTML:

 <div class="news-row">
     <article class="news-container fixed-page">  blah blah</article>
     <article class="news-container fixed-page">  blah blah2</article>
</div>



CSS:

.last {
    color: red;
}



JS:

$('article.news-container:nth-child(2n)').addClass('last');

Comments

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.