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 8282767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:29:20+00:00 2026-06-08T10:29:20+00:00

I am trying to set a custom style for elements that are ul –

  • 0

I am trying to set a custom style for elements that are

  • ul – the first in many children (ul.first)
  • last in many children (ul.last)
  • the only child (ul.first.last or ul.only)

HTML:

<ul class="selector">
    <li class="selector">some li
       <ul class="">
          <li>some fancy li - the ul parent should have first class</li>
       </ul>
       <ul class="">
          <li>some more li - the ul parent should have last class</li>
       </ul>
    </li>
    <li class="selector">some second li
        <ul class="">
          <li>some lonely li - the ul parent should have first and last classes</li>
       </ul>
    </li>
</ul>

so I came up with a piece of jQuery hoping to do the trick

$('ul.selector li.selector > ul').each(function () {
  if ($(this).is(':first') && $(this).is(':last')) {
    $(this).addClass('first last');
  } else if ($(this).is(':first')) {
    $(this).addClass('first');
  } else if ($(this).is(':last')) {
    $(this).addClass('last');
  }
});

Thanks for any suggestion.

UPDATE – here is a fiddle you can play with, based on one of the answers: http://jsfiddle.net/qAtpe/

  • 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-06-08T10:29:22+00:00Added an answer on June 8, 2026 at 10:29 am

    Why not just this :

    $('ul.selector li.selector > ul').each(function() {
        $this = $(this); // cache $(this)
        if ($this.is(':first')) {
            $this.addClass('first'); 
        } 
        if ($this.is(':last')) {
            $this.addClass('last'); 
        }
    });
    

    Then use the following CSS

    .first {
        //this is first
    }
    .last { 
        // this is last
    }
    .first.last {
        // this is first and last - ie the only child
    }
    

    Update

    $('ul.selector li.selector > ul').each(function() {
        $this = $(this); // cache $(this)
        if ($this.is(':first-child')) {
            $this.addClass('first'); 
        } 
        if ($this.is(':last-child')) {
            $this.addClass('last'); 
        }
    });
    ​
    

    Looking at your example jsfiddle – you needed the selector :first-child rather and :first ..

    The :first pseudo-class is equivalent to :eq(0). It could also be written as :lt(1). While this matches only a single element, :first-child can match more than one: One for each parent.

    This now works :

    Working example

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

Sidebar

Related Questions

I am trying to set a custom class to an Iterator through the setInfoClass
I am trying to set my AsyncTask returning a custom class type such as
I'm trying to create a custom object that behaves properly in set operations. I've
I am trying to set a custom style to a GWT CellBrowser widget using
I'm trying to make a custom button, my only problem is that the background
I am trying to set a custom title for a node add form in
I'm trying to set some custom AWS CloudWatch metrics using the Java SDK. I
I'm trying to set up a custom sort behaviour for my model/view structure in
I am trying to set up a custom formatter (named TextFieldFormatter ) for my
I'm trying to set a variable in my local.xml file for my custom block:

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.