Bhoot
Bhoot

Reputation: 400

finding the last span in DOM with a particular classname

 <div class="comments_container clearfix">
 <hr />
<ul class="comment_struct">

      <li class="commenter_pic">
    <img src="images/im.jpg" alt="picture"/>
    <span class="button blue square likes">9 Likes</span>
      </li>
      <li class="comment_data">
           <ul class="post_info_options">
        <li class="commenter_name">Abc</li>
        <li class="comment"><p>hello how are you doing</p></li>
        <li class="time">
                        <span class="comment_time">12 June 2012<>
                       <span class="time_elapsed">7 hours ago</span>
          </li>
     <li class="options">
             <span><a href="#">Once More</a></span>
             <span><a href="#">Comment</a></span>
             <span><a href="#" class="comment_like">Like</a></span>
         </li>

     <li>

     </li>
 </ul>
</li>
<li class="delete_comment"><img src="images/Cancel.png" alt="delete_post"/></li>

   </ul>
   <br/>

    <ul class="comment_struct">

      <li class="commenter_pic">
    <img src="images/im.jpg" alt="picture"/>
    <span class="button blue square likes">9 Likes</span>
      </li>
      <li class="comment_data">
           <ul class="post_info_options">
        <li class="commenter_name">Abc</li>
        <li class="comment"><p>hello how are you doing</p></li>
        <li class="time">
                        <span class="comment_time">12 June 2012<>
                       <span class="time_elapsed">7 hours ago</span>
          </li>
     <li class="options">
             <span><a href="#">Once More</a></span>
             <span><a href="#">Comment</a></span>
             <span><a href="#" class="comment_like">Like</a></span>
         </li>

     <li>

     </li>
 </ul>
</li>
<li class="delete_comment"><img src="images/Cancel.png" alt="delete_post"/></li>

   </ul>
   <br/>



    <ul class="comment_struct">

      <li class="commenter_pic">
    <img src="images/im.jpg" alt="picture"/>
    <span class="button blue square likes">9 Likes</span>
      </li>
      <li class="comment_data">
           <ul class="post_info_options">
        <li class="commenter_name">Abc</li>
        <li class="comment"><p>hello how are you doing</p></li>
        <li class="time">
                        <span class="comment_time">12 June 2012<>
                       <span class="time_elapsed">7 hours ago</span>
          </li>
     <li class="options">
             <span><a href="#">Once More</a></span>
             <span><a href="#">Comment</a></span>
             <span><a href="#" class="comment_like">Like</a></span>
         </li>

     <li>

     </li>
 </ul>
</li>
<li class="delete_comment"><img src="images/Cancel.png" alt="delete_post"/></li>

   </ul>
   <br/>

I have html structure as above .. so starting from $('.comments_container') how can i traverse to the last span in the DOM with class 'comment_time'. Help would be appreciated.

traversing using children() is possible. But i want to know is there any simpler way

Upvotes: 0

Views: 131

Answers (1)

Zbigniew
Zbigniew

Reputation: 27584

You can use :last selector:

var mySpan = $('.comments_container span.comment_time:last');

Upvotes: 2

Related Questions