Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Dec 2008
    Posts
    117
    Thanks
    14
    Thanked 0 Times in 0 Posts

    jquery: addClass

    Hi,
    How can I add class to a list and the sub-list...? I have tried the code below but it wont work...

    Code:
    $(document).ready(function(){
      $("#menu li:last").addClass('last');
      $("#menu li li:last").addClass('sublast');
    });
    this is the html code which i add the class 'last' and 'sublast' manually... the ideal situation i wish to achieve is to add these classes through jquery...

    is it possible??


    Code:
    <div id="menu">
    
    <ul>
    
    <li><a href="#">NEWS</a>
            	<ul>
                	<li><a href="#">News</a></li>
                    <li><a href="#">Opportunities</a></li>
                    <li class="sublast"><a href="#">Newsletters</a></li>
    		</ul>	
    </li>
     <li><a href="#">WORK</a>
            	<ul>
                	<li><a href="#">Past</a></li>
                    <li><a href="#">Present</a></li>
                    <li class="sublast"><a href="#">Future</a></li>
    		</ul>
    </li>
    <li class="last"><a href="#">US</a>
            	<ul>
                	<li><a href="#">About us</a></li>
                    <li><a href="#">People</a></li>
                    <li class="sublast"><a href="#">Support</a></li>
    		</ul>
    </li>
    
    </ul>
    
    </div>


    many thanks,
    Lau
    Last edited by lauthiamkok; 08-02-2009 at 11:07 PM.

  • #2
    Regular Coder Iszak's Avatar
    Join Date
    Jun 2007
    Location
    Perth, Western Australia
    Posts
    332
    Thanks
    2
    Thanked 58 Times in 57 Posts
    This should work, it did for me.
    Code:
    $('ul > li')
      .filter(':last')
        .addClass('last')
      .end()
      .find('li:last')
        .addClass('sublast');

  • #3
    Regular Coder
    Join Date
    Dec 2008
    Posts
    117
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Iszak View Post
    This should work, it did for me.
    Code:
    $('ul > li')
      .filter(':last')
        .addClass('last')
      .end()
      .find('li:last')
        .addClass('sublast');
    thank you... i got it worked with this,

    Code:
    $("#menu > ul > li:last").addClass("last");
    $("#menu > ul > li > ul > li:last-child").addClass("sublast");


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •