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

  • Home
  • SEARCH
  • 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 6567599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:21:25+00:00 2026-05-25T14:21:25+00:00

I have the folowing code: html: <div class=container> <div class=selected>A</div> <div>B</div> <div>C</div> <div>D</div> </div>

  • 0

I have the folowing code:

html:

<div class="container">
    <div class="selected">A</div>
    <div>B</div>
    <div>C</div>
    <div>D</div>
</div>
<button id="next">next!</button>

jQuery:

$("#next").click(function() {
    $(".selected").removeClass("selected").next().addClass("selected");
});

What i want is loop through the divs in the container. I can do this to cycle:

$("#next").click(function() {
    if ($(".selected").next().length == 0) {
        $(".selected").removeClass("selected").siblings(":nth-child(1)").addClass("selected");
    }
    else {
        $(".selected").removeClass("selected").next().addClass("selected");
    }
});

But i think there is a simpler way. How can i make it simpler ? (I don’t mind if you don’t use the next() function).

jsFiddle: http://jsfiddle.net/S28uC/

  • 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-25T14:21:25+00:00Added an answer on May 25, 2026 at 2:21 pm

    I ‘d prefer siblings.first() instead of siblings(":nth-child(1)"), but in essence you won’t be able to wrap around without using some variant of next().length.

    Update: If I were writing this from scratch, this is how I ‘d do it:

    $("#next").click(function() {
        var $selected = $(".selected").removeClass("selected");
        var divs = $selected.parent().children();
        divs.eq((divs.index($selected) + 1) % divs.length).addClass("selected");
    });
    

    This approach is motivated by two factors:

    1. When you want to cycle over a collection indefinitely, modulo comes to mind
    2. Getting rid of the if makes for smarter-looking code

    When setting the value of divs I preferred $selected.parent().children() over the equivalent $selected.siblings().add($selected) as a matter of taste — there are practically endless possibilities.

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

Sidebar

Related Questions

I have the following HTML code: <div class=container> <span class=red></span> <span class=hot></span> </div> On
I have the following simple code, where I double click a div (with class
I have the following HTML code: <div class=outer_container> <div id=imgContainer> <img src=/some/image /> </div>
I have the following code $(document).ready(function(){ // class exists if($('.delete').length) { // add click
I have the following html; <div class=event-details-container> <div class=event-details> <div class=adress>My adress</div> <div class=arranger>My
I have the following code in my file to load a div with HTML
I have the following jQuery function: function GetGrandparentDiv(item) { return item.parents('div:eq(1)'); } Which is
I currently have the following code set up for a jQuery slide. $(document).ready(function ()
I have the following div structure, but this structure is changing <div class=rt-container> <div
I have the following code: $(.main).click(function() { $(this).toggleClass(classA).toggleClass(classB); $(.secondary).toggleClass(classC).toggleClass(classD); )); $(.secondary).click(function() { $(.secondary).toggleClass(classC).toggleClass(classD); ));

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.