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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:16:16+00:00 2026-06-18T04:16:16+00:00

I have a list of <li> elements, and I’m using :nth-child to target specific

  • 0

I have a list of <li> elements, and I’m using :nth-child to target specific children of every 3rd and 4th element, and give them an additional class .right.

But, when I hide some elements (let’s say, the first or second list item), it doesn’t work properly. I want to add class “right” only to children of visible list items. How can I do that?

This is the code I’m using:

$(".ilist:visible:nth-child(4n+3)").find(".window").addClass("right");
$(".ilist:visible:nth-child(4n+4)").find(".window").addClass("right");

And I remove all classes .right with this:

$(".ilist .window.right").removeClass("right");

HTML structure:

<ul>
   <li class="ilist">
      ...
      <div class="name">Name (Search works with it)</div>
      <div class="window"></div>
   </li>

   ...
</ul>
  • 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-18T04:16:17+00:00Added an answer on June 18, 2026 at 4:16 am

    The fact that you hide an element does not change its position in the DOM tree, so nth-child(4n+3) won’t change if you hide any sibling list item.

    When you toggle the visibility some elements list items, you’ll have to reset your right classes, and reapply to the appropriate elements.

    You can do that by passing a function to addClass, instead of the new class name. This function will receive the index of the matched element as the first parameter, and you can use it to check if it’s the 3rd or 4th. If it is, return the new class name you want to apply:

    $(".ilist .window.right").removeClass("right");
    $(".ilist:visible .window").addClass(function(i, currentClass){
        return (i == 2 || i == 3) ? "right" : "";
    });
    

    Live demo at http://jsfiddle.net/6TMmJ/

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

Sidebar

Related Questions

I have a list of elements, and I've been using will_paginate up until now,
I have a list of xml elements that i need to search for specific
I'm having trouble to group elements inside a list element. For excample I have
I have a list of elements in Erlang, and I'm using lists:foreach to traverse
I have a list of elements around 1000. Each element (objects that i read
I have some list elements with dynamically id changing. Next i give this id's
I have list of elements and when I choose element from list, "value" from
I have a list of elements. for each element I want to split into
I have a list with child elements like : <ul> <li>.. </li> <li> <ul>
I have list elements like so: <li> <strong style=color: #BBB;>1</strong> <a href=>Matt</a> <b>4 days

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.