Samuel Luke Anthony
Samuel Luke Anthony

Reputation: 47

jquery traversing dom

How do I select the last div id blog_comments in this row?

    <div id="blog_comments">
        <div class="name"><span class="blog_bold">By: </span>SamSambinks</div>
    </div>

    <div id="blog_comments">
        <div class="name"><span class="blog_bold">By: </span>SamSambinks</div>
    </div>

    <div id="blog_comments">
        <div class="name"><span class="blog_bold">By: </span>SamSambinks</div>
    </div>

Sounds like a simple question I've tried: $("#blog_comments:last") and $("#blog_comments").last

and others but nothing seems to work?

Upvotes: 0

Views: 68

Answers (2)

Gabe
Gabe

Reputation: 50493

The reason why it's not working is id needs to be unique. Make them classes instead of creating non-unique id's, having duplicate id's is actually invalid markup.

<div class="blog_comments">
   <div class="name"><span class="blog_bold">By: </span>SamSambinks</div>
</div>

<div class="blog_comments">
   <div class="name"><span class="blog_bold">By: </span>SamSambinks</div>
</div>

<div class="blog_comments">
   <div class="name"><span class="blog_bold">By: </span>SamSambinks</div>
</div>

Then your selector:

$('.blog_comments').last();

Working Example here

Upvotes: 4

klob
klob

Reputation: 687

You can't have multiple DOM nodes with same id. Try to change id="blog_comments" to class="blog_comments".

Upvotes: 1

Related Questions