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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:22:05+00:00 2026-05-15T18:22:05+00:00

Im trying to use jQuery to add a different class for each Span element,

  • 0

Im trying to use jQuery to add a different class for each Span element, using .next()
This is the javascript:

<script type="text/javascript">

    $(function() {
        $("a", "div.top_menu").addClass("ui-icons-custom");

        $("span.ui-icon:first", "ul.top_menu_list").addClass("ui-icon-pencil")
            .next().addClass("ui-icon-comment")
            .next().addClass("ui-icon-key");
    });

</script>

And this is the HTML Im trying to make it work:

<div class="top_menu ui-state-default-custom">
                <ul class="ui-widget top_menu_list">

                    <li>
                        <span style="float: left;" class="ui-icon-text-custom"><span style="float: left;" class="ui-icon"></span>
                            <a href="test">Registrarse</a>
                        </span>
                    </li>
                    <li>
                        <span style="float: left;" class="ui-icon-text-custom"><span style="float: left;" class="ui-icon"></span>
                            <a href="test1">Agregar un anuncio</a>
                        </span>
                    </li>
                    <li>
                        <span style="float: left;" class="ui-icon-text-custom"><span style="float: left;" class="ui-icon"></span>
                            <a href="test2">Ingresar (Login)</a>
                        </span>
                    </li>
                </ul>
            </div>

So it should take the first ‘span’ element with .ui-icon class and add a class, then the next from the list and add other class.

  • 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-15T18:22:06+00:00Added an answer on May 15, 2026 at 6:22 pm

    .next() finds the next immediate sibling to the element that you selected. In your case, the span you selected has an anchor as its next sibling, and then nothing. You’d have to go back up to the parent li using parent(“li”), use next() to get to the next li in the list, then use find(“span span”) to get to the span in question.

    It’s probably easier and a lot more readable to select all the spans and then edit each individually, e.g.:

    $(function() {
        $("a", "div.top_menu").addClass("ui-icons-custom");
    
        var $spans = $("span.ui-icon", "ul.top_menu_list");
    
        // end() reverts us back to the selector before each eq()
        // you can just make this three separate statements if you like
        $spans.eq(0).addClass("ui-icon-pencil").end()
              .eq(1).addClass("ui-icon-comment").end()
              .eq(2).addClass("ui-icon-key");
    });
    

    Er, and out of curiosity, what’s stopping you from adding the classes to the elements directly rather than using jQuery to do it?

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

Sidebar

Related Questions

I'm using (trying to use) jQuery to add a class to the first paragraph
I am trying to use jquery to add a class to my css menu
I am trying to use jQuery to add a hover opacity on an element
I'm trying use jquery to remove a class from an element (not knowing ahead
i'm trying to use jQuery multiselect plugin in a form editing jqGrid (add form).
I am need paint my image. I'm trying use JQuery in here this link:
I'm trying to use jQuery to format code blocks, specifically to add a <pre>
I am trying to use Javascript/jQuery to resize part of a website based on
I am trying to use jQuery to add a div around the following code:
I am trying to use jquery autocomplete and custom html to add a image

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.