Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6357729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:13:27+00:00 2026-05-24T23:13:27+00:00

I would like to target the first and last ‘li’ of a ‘ul’ using

  • 0

I would like to target the first and last ‘li’ of a ‘ul’ using jQuery and add a class of “first” and “last” to them accordingly.

I know that using jQuery this is easy if its just a simple ‘ul’ but I am struggling with the right syntax if the ‘ul’ has more than one ‘ul’ nested in the parent ‘ul’.

Here is an example of the html markup:

<ul>
    <li><a href="#">Top Level Item</a>
        <ul>
            <li><a href="#">First Sub-Item</a></li>
            <li><a href="#">Last Sub-Item</a>
                <ul>
                    <li><a href="#">First Sub-Sub-Item</a>
                    <li><a href="#">Last Sub-Sub-Item</a></li>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

I need to target all of the first and last items of each sub-ul and apply a class to each of them.

Here is what I have been using but it is obviously not working as it is applying the class name to only the very first ‘li’ and the very last ‘li’.

jQuery('.topmenu ul li ul li').first().addClass('first');
jQuery('.topmenu ul li ul li').last().addClass('last');

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:13:28+00:00Added an answer on May 24, 2026 at 11:13 pm

    I’ll assume that everything you have shown is inside something with the class “topmenu”. I would advise changing this to an ID if it’s unique, and applying a class to the “first ul” so it doesn’t have to be avoided algorithmically.

    $('.topmenu').find('ul ul li:first-child').addClass('first');
    $('.topmenu').find('ul ul li:last-child').addClass('last');
    

    http://jsfiddle.net/aBksB/2/

    EDIT: to reflect comment

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In specializing a class template, I would like to have one specialization target full-blown
Would like a for loop in jquery so that: For every hover_link: show hidden
I would like to make rounded corners for the first and last cell of
I have several paragraphs that I would like to indent, although only the first
I would like to be able to cycle anchor selection using jQuery 1.4.4. The
I would like to know a way that it will show a pre-load.gif in
First off I would like to say, that I am not trying to hack
I am developing an Android app in Eclipse. I would like to target a
I would like to change the target of symbolic link from within a bash
I'm developing my Android app with target set to 15, but would still like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.