Every third item is selected using a JQuery selector

Currently, I am tackling the task of working with PHP loops to generate multiple li elements within a single ul. The issue arises when I attempt to display every fourth li upon clicking one of the preceding three li.

My current implementation only toggles the class for the first preceding li as shown below:

$('.last_news li').on('click', function(){

    $('+ .actu_details',this).toggleClass('expend');

});

Seeking advice from the community for any insights on this matter.

$('.last_news li').on('click', function() {

    $('+ .actu_details', this).toggleClass('expend');

});
.last_news {
    padding: 35px
}

ul {
    padding-left: 0px;
    margin: 0;
    overflow: hidden;
}

ul li {
    list-style-type: none;
    cursor: pointer;
    float: left;
    width: 33%;
    height: 250px;
    background-color: red;
    margin-right: 0.5%;
    margin-bottom: 5px;
    color: #FFF;
    position: relative;
}

li:nth-of-type(3) {
    margin-right: 0;
}

li:nth-of-type(4n+7) {
    margin-right: 0;
}

li.actu_details {
    width: 100%;
    height: 0px;
    background-color: green;
    display: block;
}

li.actu_details.expend {
    height: 350px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="last_news">
    <div class="contenu_grid">
    <ul>
        <li class="une_actu">Test</li>
        <li class="une_actu">Test</li>
        <li class="une_actu">Test</li>
        <li class="actu_details">
            Detail
        </li>
        <li class="une_actu">Test</li>
        <li class="une_actu">Test</li>
        <li class="une_actu">Test</li>
        <li class="actu_details">
            Detail
        </li>
        <li class="une_actu">Test</li>
        <li class="une_actu">Test</li>
        <li class="une_actu">Test</li>
        <li class="actu_details">
            Detail
        </li>
    </ul>
    </div>
</section>

Answer №1

To target the first li element, you can utilize the nextAll() function to select the following sibling actu_details element, followed by using :first, :eq(0), .eq(0), or .first().

$('.last_news li').on('click', function () {
    $(this).nextAll('.actu_details:first').toggleClass('expend');
    //$(this).nextAll('.actu_details').eq(0).toggleClass('expend');
});

 $('.last_news li').on('click', function() {
   $(this).nextAll('.actu_details:first').toggleClass('expend');
 });
.last_news {
  padding: 35px
}

ul {
  padding-left: 0px;
  margin: 0;
  overflow: hidden;
}

ul li {
  list-style-type: none;
  cursor: pointer;
  float: left;
  width: 33%;
  height: 250px;
  background-color: red;
  margin-right: 0.5%;
  margin-bottom: 5px;
  color: #FFF;
  position: relative;
}

li:nth-of-type(3) {
  margin-right: 0;
}

li:nth-of-type(4n+7) {
  margin-right: 0;
}

li.actu_details {
  width: 100%;
  height: 0px;
  background-color: green;
  display: block;
}

li.actu_details.expend {
  height: 350px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="last_news">
  <div class="contenu_grid">
    <ul>
      <li class="une_actu">Test</li>
      <li class="une_actu">Test</li>
      <li class="une_actu">Test</li>
      <li class="actu_details">
        Detail
      </li>
      <li class="une_actu">Test</li>
      <li class="une_actu">Test</li>
      <li class="une_actu">Test</li>
      <li class="actu_details">
        Detail
      </li>
      <li class="une_actu">Test</li>
      <li class="une_actu">Test</li>
      <li class="une_actu">Test</li>
      <li class="actu_details">
        Detail
      </li>
    </ul>
  </div>
</section>

Fiddle

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Running Discord.js RPC on a GTA or Minecraft server

Can a Rich Presence be hosted on a Minecraft server or an Alt:v GTA server so that all players who join have it in their status? ...

How can you retrieve the <head> content from a remote website without being able to make any changes to that site?

Imagine I need to access the <head> tag of a remote website like YouTube. Whenever I attempt this, I encounter an error message: Access to XMLHttpRequest at ‘Website I am trying to access for the head section’ from origin ‘My Website’ has b ...

Sending Paypal IPN Data to Node.JS: A Step-by-Step Guide

I'm looking to implement a real-time donation system on my website that can update all users simultaneously. The challenge I'm facing is that the IPN (Instant Payment Notification) page, responsible for verifying payments, is written in PHP. Unf ...

If the navigation menu contains sub-menus, it should slide to the left

I am currently working on developing a navigation menu with four levels of depth. The menu displays three levels at a time, and when the user reaches the fourth level, the first level should move to the left to make room for the second, third, and fourth l ...

Exploring the keyof operator in Typescript for object types

Is there a way to extract keys of type A and transfer them to type B? Even though I anticipate type B to be "x", it seems to also include "undefined". Why does the keyof operator incorporate undefined in the resulting type? It's perplexing. I kn ...

The synergy of THREEJS and GC in creating dynamic and interactive

In an effort to optimize my threejs scene, I noticed that the CPU usage was high even when the scene was not in use. After exploring various solutions, I tried removing the memory of mesh using the dispose() method to prevent memory leaks. labelMesh[i].ge ...

Tips for avoiding a form reload on onSubmit during unit testing with jasmine

I'm currently working on a unit test to ensure that a user can't submit a form until all fields have been filled out. The test itself is functioning correctly and passes, but the problem arises when the default behavior of form submission causes ...

Obtaining the body from a post request in Node.js: A step-by-step guide

When sending data with the id using the post method to my node server, I encountered an issue where req.body is returning as undefined in my node file. This is what my index.html looks like: <html ng-app="IMDBApp"> <body> <div ng-contr ...

Techniques for transferring checkbox values to a JavaScript function

Here is the code snippet I am working with: <input type="checkbox" name="area" id="area" value="0">Some Text1</input> <input type="checkbox" name="area" id="area" value="80">Some Text2</input> Additionally, here is the JavaScript ...

Passing a Ruby session variable to a JavaScript tag: a step-by-step guide

I'm currently collaborating with a vendor who utilizes a JavaScript tag for sale attribution, and I need to pass session variables to the tag. The tag appears to be firing properly, as I can see the variables in the logs, but they aren't reflecte ...

Custom control unable to display MP3 file

Hey, I came across this awesome button that I am really interested in using: https://css-tricks.com/making-pure-css-playpause-button/ I'm currently facing two issues with it. First, I can't seem to play the sound. I've placed the mp3 file ...

Creating a dynamic polyline with custom data on Mapbox is a great way to enhance your mapping experience

Hey everyone, I'm looking to create a polyline or path on Mapbox using my own data. I came across an example on mapbox.com that shows how to draw a sine wave on the map. How can I customize this example to use my own data? <!DOCTYPE html> <h ...

Applying CSS borders with circular corners

Can this unique border style be achieved using only CSS? https://i.sstatic.net/wMtbG.png ...

How can I eliminate a class when the scrollTop() position matches a specific div element?

I am trying to implement a script that will allow me to scroll to my navigation and then change the class of the navigation, however, it is not working as expected. $(window).scroll(function() { if ($(window).scrollTop == $('.menu_nav').offs ...

Secure Access with Skype Bot Verification

Recently, I managed to set up a NodeJS bot to synchronize messages between Discord and Skype chats. Although I am relatively new to Javascript and completely unfamiliar with NodeJS, the existence of a framework called Spype has been quite beneficial. The i ...

CSS3 stackable-design

How can I avoid CSS3 elements from overlapping? They are all <span> precious </span> <span> and </span> <span> cute </span> And here is my current CSS code span { margin : 20px; min-width: 150px; border-r ...

Using Angular to dynamically access component properties

Seeking assistance with creating dynamic Tabs in TabView of PrimeNG. The components are displaying properly, but I am unsure how to access their properties. I am following the guidelines provided at https://angular.io/guide/dynamic-component-loader and us ...

Arrangement of 3 columns on the left and 1 column on the right using flex-box layout

My goal is to utilize flexbox in conjunction with bootstrap 4 to produce a design featuring 3 vertically stacked columns on the left, accompanied by a single column on the right that matches the height of the left columns. Following that, there will be som ...

JavaScript Variable Naming ConventionEnsuring correct spelling in variable names is essential

While I may not be a JavaScript expert, I have a question about its dynamically typed nature. We are all aware that JavaScript (node.js) is a dynamically typed language where we can easily assign values like: someObject.attr = 123; However, due to the la ...

A guide to storing a JavaScript variable in a MySQL database using Express.js

Looking for some guidance on node js and expressjs framework. While developing a web application, I've encountered an issue with saving data into the database. Everything seems to be set up correctly, but the data stored in the variable (MyID) is not ...