Click here to Skip to main content
Click here to Skip to main content

Adding a Style to the Last li Tag with Jquery

By , 21 Nov 2012
 

Introduction

This is a simple problem not usually faced by everyone, but I had a situation to face a problem like this. It's about finding the last li tag from my HTML code which is inside a ul tag.

Using the Code

Here is the screen shot of my problem. The problem is that I needed to add a style to the last li tag to remove the underline to mean that it's the ending li.

I searched a lot to find a solution for this problem and got a nice solution that I wanted to share.

Here is the screen shot of my problem:

Here goes the HTML code for my problem:

<ul id="LeftSide">
    <li>
        <h5>
            Categories
        </h5>
    </li>
    <li>
        <h5>
            Pierogi and Dumplings
        </h5>
    </li>
    <li>
        <h5>
            Sausages11
        </h5>
    </li>
    <li>
        <h5>
           Prepared Polish Dishes
        </h5>
    </li>
    <li>
        <h5>
           Test-Category
        </h5>
    </li>
    <li>
        <h5>
           Bakery2
        </h5>
    </li>
    <li>
        <h5>
           Pantry2
        </h5>
    </li>
     <li>
        <h5>
           Gift Baskets
        </h5>
    </li>
    <li>
        <h5>
           Test2
        </h5>
    </li>
</ul>  

The above is the HTML code. Here I would like to add a class for my last li tag. That is for my code "Test2" will be of something different. We can add border, change font style and do whatever we like to do.

Here the solution goes:

<script type="text/javascript">
        $("#LeftSide li:last").addClass("border-last"); 
 </script> 

Here is the CSS style that I wanted to add with my last "li" tag to remove the bottom border:

.border-last 
{
    border-bottom: none !important;
} 

Here "LeftSide" is the id of the "ul" tag and "border-last" is the CSS class we like to add with the last li tag. If we do this, the HTML code will be like this:

<ul id="LeftSide">
    <li>
        <h5>
            Categories
        </h5>
    </li>
    <li>
        <h5>
            Pierogi and Dumplings
        </h5>
    </li>
    <li>
        <h5>
            Sausages11
        </h5>
    </li>
    <li>
        <h5>
           Prepared Polish Dishes
        </h5>
    </li>
    <li>
        <h5>
           Test-Category
        </h5>
    </li>
    <li>
        <h5>
           Bakery2
        </h5>
    </li>
    <li>
        <h5>
           Pantry2
        </h5>
    </li>
     <li>
        <h5>
           Gift Baskets
        </h5>
    </li>
    <li class="border-last">          // Here the Class is added with the last li
        <h5>
           Test2
        </h5>
    </li>
</ul>  

Here is the screen shot of my solution that removes the bottom border from the last "li" tag.

Happy coding!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

dpalash
Software Developer Desme Bangladesh
Bangladesh Bangladesh
Member
I have completed my Undergraduate from Khulna University of Engineering in Computer Science & Engineering. Now i am working as a Software Engineer at desme-Bangladesh on ASP.NET. I really love this technology and like to build my career with this only.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberMonjurul Habib23 Nov '12 - 7:14 
GeneralRe: My vote of 5memberdpalash23 Nov '12 - 20:07 
GeneralMy 5memberShahriar Iqbal Chowdhury22 Nov '12 - 18:49 
GeneralRe: My 5memberdpalash22 Nov '12 - 19:49 
GeneralMy vote of 5memberMunir Hassan22 Nov '12 - 16:10 
GeneralRe: My vote of 5memberdpalash22 Nov '12 - 19:49 
Suggestionuse instead css pseudo-class or adjacent selectormemberBen.12421 Nov '12 - 22:36 
GeneralRe: use instead css pseudo-class or adjacent selectormemberdpalash22 Nov '12 - 0:56 
SuggestionUse CSSmemberRodkin Semen21 Nov '12 - 20:48 
GeneralRe: Use CSSmemberdpalash22 Nov '12 - 0:59 
QuestionCSSmemberCaner Korkmaz21 Nov '12 - 5:47 
AnswerRe: CSSmemberdpalash22 Nov '12 - 1:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 22 Nov 2012
Article Copyright 2012 by dpalash
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid