Use jQuery to assign a unique CSS class to every third ul element on the page

Currently, I have a large number of li elements that I am iterating through and grouping in sets of 5. However, I need to assign a unique class to every third group in order to achieve the following structure:


        <ul class="class1">
            <li></li><li></li> <li></li><li></li><li></li>
        </ul>
        <ul class="class2">
            <li></li><li></li><li></li><li></li><li></li>
        </ul>  
        <ul class="class3">
            <li></li><li></li><li></li><li></li><li></li>
        </ul>  

        <ul class="class1">
            <li></li><li></li><li></li><li></li><li></li>
        </ul>  
        <ul class="class2">
            <li></li><li></li><li></li><li></li><li></li>
        </ul>  
        <ul class="class3">
            <li></li><li></li><li></li><li></li><li></li>
        </ul>

        <ul class="class1">
            <li></li><li></li><li></li><li></li><li></li>
        </ul>  
        <ul class="class2">
            <li></li><li></li><li></li><li></li><li></li>
        </ul>  
        <ul class="class3">
            <li></li><li></li><li></li><li></li><li></li>
        </ul> 
    

I have written a script using jQuery to group these elements as desired:


        var lis = $(".leftHover ul li");  
        for(var i = 0; i < lis.length; i += 5) { 
            lis.slice(i, i + 5).wrapAll("<ul></ul>");
        }
    

Answer №1

Implement a method to apply a different CSS class every 3rd <ul> element using jQuery.

To achieve this, you can utilize the nth-child selector in the following manner:

$(".leftHover ul li:nth-child(3n)").addClass('className');

This code snippet will add the specified class to every third <li> element within an element with the class of `leftHover`. You are free to modify the argument of `3` as needed based on your specific requirements.

Answer №2

Is it possible to simply...

var items = $(".leftHover ul li");  
for(var j = 0; j < items.length; j+=5) { 
  items.slice(j, j+5).wrapAll('<ul class="class' + (j%3) + '"></ul>');  
}

This will label them in the reverse order... class2, class1, class0 instead of class1, class2, class3. To achieve class1, class2, class3 sequencing, you can modify (i%3) to (3-(i%3)).

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

reveal concealed section and seamlessly glide to specified location inside the secret compartment

I have implemented a feature on my website where hidden divs are revealed one at a time, with the screen scrolling to display each specific div. While this code functions well, I am now looking to enhance it by opening the hidden div and smoothly scrolling ...

Having trouble with updating your website asynchronously through code? In need of a detailed explanation?

Here are two Javascript functions that are used to call a python file in order to update an HTML page. The first function works perfectly fine, but the second one seems to be malfunctioning without throwing any errors. function button(key) { var xhttp ...

Is there a way to make this eval() function function properly in Internet Explorer?

My JavaScript code is fetching another JavaScript "class" from a different XHTML page. The fetched JavaScript looks like this: (function() { this.init = function() { jQuery("#__BALLOONS__tabs").tabs(); }; }) Once the f ...

To access a form element in JavaScript function, utilize the dynamic form name approach

Currently, I am facing an issue with a JavaScript alert function where I am trying to view the HTML form URL before it is submitted. Following advice from a post on StackOverflow about retrieving form submit content before submit, I attempted to implement ...

The Bootstrap validator triggers the form submission only after the second click

When I click on the submit button, I am attempting to submit a form that has its default action prevented and first checks a condition before proceeding. Below is the code snippet: $('#payment-form').bootstrapValidator({ live: 'dis ...

What causes certain images to have unspecified height and width measurements?

Currently, I am in the process of extracting image sizes from a website using a module called scraperjs. Most images have their height and width attributes defined, but some do not, resulting in the returned value being "undefined". How can I retrieve the ...

Perform an Ajax request upon clicking on the dropdown list item

I recently created a drop-down list using some JavaScript code. Here's how I did it: <script> $('.menu').dropit(); </script> <ul class="menu"> <li> <a href="#">Product_name</a> ...

What is the best way to decrease the font size of every element in the DOM?

Is there a way to decrease the size of all DOM elements by a specific amount? Currently, I am using Bootstrap styles where h5 is set at 14px and I need it to be 12px, and h1 is at 36px when I want it to be 34px, and so forth. I have considered two option ...

Adding an existing element to the DOM using Javascript/jQuery

I am facing an issue in my DOM where I have an element with two children, a div block and a button. My goal is to add a new copy of the same div block to the parentNode whenever the button is clicked. Currently, I am using the following code in the button ...

Sending HTML data using jQuery's post method

TestData = { Test: function (eventId) { var obj = new Object(); obj.Content = 'Hello<br>world.'; var data = $.toJSON(obj); alert(data); $.post(SvConstant.GetBaseUrl() + "/Services/PageHandler/Tes ...

Utilizing CSS styling to create page breaks in R Markdown disrupts the flow of table of contents

Encountering difficulties in achieving a visually pleasing document using knitr's export to pdf, I have resorted to converting the html file for my Markdown project to pdf through the wkhtmltopdf command line utility tool. A simplified Markdown docum ...

The custom attribute in jQuery does not seem to be functioning properly when used with the

I am currently working with a select type that includes custom attributes in the option tags. While I am able to retrieve the value, I am experiencing difficulty accessing the value of the custom attribute. Check out this Jsfiddle for reference: JSFIDDLE ...

The issue with `allowInputToggle` not functioning in a Bootstrap 4 project needs to be addressed in tempusdominus

I recently started using tempusdominus and was trying to implement the allowInputToggle property. More information can be found here: The objective is to have the calendar open when the user clicks into an <input>, instead of having to click on the ...

Tips for using jQuery to add or append a class to the final li element

I am currently struggling to understand why the last child of the li element is not being inserted into the last li. This module functions as a treeview, and my main objective is to add a class to the last li. Let me demonstrate with some sample code and ...

Angular2 Components with Unique Styling

I am working on an Angular2 app that includes several components, some of which I want to reuse multiple times on a single page. Instead of having three separate components, I am looking to refactor and combine their functionalities into one. The basic st ...

Ways to conceal the contents of an HTML element

I recently applied a background image to my "a" tag, but now I'm looking to hide only the HTML content within the tag. It's important that the background image and href address remain visible. Any suggestions on how to achieve this? Thank you in ...

Issue with AJAX loading functionality not functioning properly in Internet Explorer

For a project, I have a portfolio that I need to create. The front page consists of a div with a loader which determines the screen size upon landing and selects the content to be pulled in using ajax. The reason for this approach is due to the simplicity ...

Toggle visibility of div content when hovering over a link by leveraging the data attribute, with the div initially visible

I have a collection of links: <p><a href="#" class="floorplan initial" data-id="king"><strong>King</strong></a><br> 4 Bedrooms x 2.5 Bathrooms</p> <p><a href="#" class="floorplan" data-id="wood">< ...

Deactivate onclick action in the <div class="images"> when using an external link with the Jquery Tools Slideshow Plugin

As a newcomer to jQuery, I am encountering an issue with the Jquery Tools Slideshow Plugin. If anyone could offer some assistance, I would greatly appreciate it. In my scenario, I have multiple links within an HTML block (similar to the example below). I ...

Issue: Node.js Express unable to access static files when the route is nested more than one folder level deep.Resolution

Before we delve into the question, let me share with you the folder structure of my node.js express app: /home server.js index.html /app /routes public.js /public /css main.css In my server.js file, ...