views:

114

answers:

3

My Html mark-up is somthing like this

<div>
    <div></div>
    <div></div>
    <div></div>
    <div class="last">
         <div class="a">
                 <div class="b">
                        <div class="c"></div>
                 </div>
         </div>

    </div>

</div>

Want to add a extra class in <div class="last">,<div class="a">,<div class="b">, and <div class="c">. Using jQuery

+1  A: 
$('div:last').addClass('yourclass');
jAndy
A: 

See Demo Here :)

Last div:

$('div.last').addClass('class');

Last div of the last div:

$('div.last').find('div:last').addClass('class');

See more info about:

addClass
:last

Sarfraz
A: 

Better than :last is :last-child: http://api.jquery.com/last-child-selector/

$("div:last-child").addClass("class");
// this gets both the last div and the last div of another div

The difference being twofold:

  1. :last-child can match more than one element; e.g., one per parent element, so that if your selector is complex at all, it will match everything you mean to match. (See page for example.)
  2. :last-child coincides with CSS2 terminology, so you can style just like you select in jQuery.

Examples

From what you've said, you could select what you want like this:

$("body > div:first-child > div:last-child").addClass("last")
  .children("div").addClass("a")
  .children("div").addClass("b")
  .children("div").addClass("c");

You don't actually need the "div" inside .children(), but I'm just demonstrating that you can filter the results--only DIVs that are children of each respective level would get the class.

D_N
Wants to add different different class..
For a example i wrote a class,the thing is that at the run time there is no class to any one div.
What classes do you want to add, exactly? Or are you just wondering how to select each one? :last-child will select all of those, I believe, but won't allow you to stop and add a different class per level. Is that what you want?
D_N
any different classes to etch.
I'm sorry, I'm having a hard time understanding what you're doing. Perhaps this will help: to learn about navigating HTML elements generally in jQuery, see selection http://api.jquery.com/category/selectors/ and traversing http://api.jquery.com/category/traversing/ on the jQuery site. Anything can be selected; it's very powerful, but you'll have to ask specific questions for specific answers.
D_N
I'm adding a couple examples to my answer to see if they start you in the right direction.
D_N
No comments for the downvote? My answer does more to answer the OP than either of the other ones so far.
D_N
Hey thank's its working fine :-)
@saorabh Great, glad to hear! If this answered your question, can you click the 'checkmark' next to my answer?
D_N