Click here to Skip to main content
15,881,657 members
Articles / Web Development / HTML
Tip/Trick

Adding a Style to the last li tag with jQuery

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
21 Nov 2012CPOL1 min read 26.1K   3   12
Here in this tip, we will be able to find the last li tag and put a style with it.

Introduction

This is a simple problem not usually faced by everyone, but I had a situation where I faced 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 screenshot 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 screenshot of my problem:

Here goes the HTML code for my problem:

HTML
<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 something different. We can add border, change font style, and do whatever we like to do.

Here is the solution:

HTML
<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:

CSS
.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:

HTML
<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>
    // Here the Class is added with the last li
    <li class="border-last">
        <h5>
           Test2
        </h5>
    </li>
</ul>  

Here is the screenshot 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)


Written By
Software Developer Cefalo
Bangladesh Bangladesh
Hi,

I am Palash Debnath. I have been working on windows technologies since 2008. I started with ASP.NET. Then I moved to Windows Form and from the last year I have been working with Windows 8 app development. Work with Windows 10 apps development as well. Now I have been working with Microsoft Azure. I have completed my Undergraduate from Khulna University of Engineering in Computer Science & Engineering. Currently working as a Senior Software Engineer at Cefalo.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Monjurul Habib23-Nov-12 7:14
professionalMonjurul Habib23-Nov-12 7:14 
GeneralRe: My vote of 5 Pin
dpalash23-Nov-12 20:07
professionaldpalash23-Nov-12 20:07 
GeneralMy 5 Pin
Shahriar Iqbal Chowdhury/Galib22-Nov-12 18:49
professionalShahriar Iqbal Chowdhury/Galib22-Nov-12 18:49 
GeneralRe: My 5 Pin
dpalash22-Nov-12 19:49
professionaldpalash22-Nov-12 19:49 
Thanks Smile | :)
GeneralMy vote of 5 Pin
Munir Hassan22-Nov-12 16:10
Munir Hassan22-Nov-12 16:10 
GeneralRe: My vote of 5 Pin
dpalash22-Nov-12 19:49
professionaldpalash22-Nov-12 19:49 
Suggestionuse instead css pseudo-class or adjacent selector Pin
Ben.12421-Nov-12 22:36
Ben.12421-Nov-12 22:36 
GeneralRe: use instead css pseudo-class or adjacent selector Pin
dpalash22-Nov-12 0:56
professionaldpalash22-Nov-12 0:56 
SuggestionUse CSS Pin
Rodkin Semen21-Nov-12 20:48
Rodkin Semen21-Nov-12 20:48 
GeneralRe: Use CSS Pin
dpalash22-Nov-12 0:59
professionaldpalash22-Nov-12 0:59 
QuestionCSS Pin
Caner Korkmaz21-Nov-12 5:47
Caner Korkmaz21-Nov-12 5:47 
AnswerRe: CSS Pin
dpalash22-Nov-12 1:06
professionaldpalash22-Nov-12 1:06 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.