Apply the jQuery method to select elements with index 3, 4, 5, 6, or 7, then add

My current Wordpress blog displays 20 posts.

I am looking to apply a specific class to posts numbered 2 through 10, and then again from 12 to 20. I do not want the first post or post number 11 to have this class added.

I attempted using CSS selectors like nth-child(3n+1), but that did not provide the desired outcome.

$('.article-box:nth-child(1n+3)').addClass('small-river');

I also experimented with .slice(2,3,4,5,6,7,8) but was unable to achieve the desired repetition effect.

$('.article-box').slice(3,4,5).addClass('small-river');

Answer №1

To exclude specific indexes, add them to the following array. Please note that the indexing starts at 0.

 var skipIndex = [1, 2, 10, 11];

$('.article-box').each(function(index) {
  
  if(skipIndex.indexOf(index) == -1) {
    $(this).addClass('small-river');
  }
  
});
.small-river {
  background: #0f0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <div class="article-box">Lorem ipsum</div>
    <div class="article-box">Lorem ipsum</div>
    <div class="article-box">Lorem ipsum</div>
    <div class="article-box">Lorem ipsum</div>
    <div class="article-box">Lorem ipsum</div>
    <div class="article-box">Lorem ipsum</div>
    <div class="article-box">Lorem ipsum</div>
    <div class="article-box">Lorem ipsum</div>
    <div class="article-box">Lorem ipsum</div>
    <div class="article-box">Lorem ipsum</div>
    <div class="article-box">Lorem ipsum</div>
    <div class="article-box">Lorem ipsum</div>
    <div class="article-box">Lorem ipsum</div>
    <div class="article-box">Lorem ipsum</div>

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

"Feeling puzzled by the intricacies of the Jquery Timer

I am currently using the jQuery Timer plugin from to dynamically generate documents every 5 seconds with pause and resume functionality. Below is my script: $(function() { $('.handler').load("../Pages/csFetchCustomer.ashx?"); ...

Creating a Custom Class for a Custom Button in TinyMCE 4 Using addButton()

Is there a way to add a custom class to a custom button using the addButton() function in TinyMCE? Here is an example: editor.addButton('keywords', { text: 'Insert Keywords', class: 'MyCoolBtn', ...

What could be causing the empty space on the right side of my container?

Currently, I'm tackling a project using only HTML, CSS, and Bootstrap. Things were going smoothly until I stumbled upon an unexpected gap on the right side of my container. I'm puzzled as to why this is happening and how I can go about rectifying ...

A timer created using jQuery and JavaScript

Looking for a way to automatically transition between three div elements with a fade in/out effect every 8 seconds? Check out this simple code snippet I wrote: $(".link1").click(function () { $(".feature1").fadeIn(1000); $(".feature2").fadeOut(1000) ...

Looking to create cascading dropdown options

I am working on a form that includes 3 dropdowns with the same options listed below: <select id="one"> <option value="1">Select</option> <option value="1">One</option> <option value="2">Two</option> <option val ...

Employ jQuery to set values for hidden input elements

I'm facing an issue where I need to assign a value to a hidden field on a form that I attach to a div when a button is clicked. Below is the snippet of my HTML code: <div style="margin-bottom:5px" class="comment" data-commentID="<?php echo $c- ...

Troubleshooting JSONP Implementation with JQuery: Scope versus Async Problems

I've been trying to call a JSONP service using the $.ajax function: $.ajax({ url: jsonpURI, dataType: "jsonp", jsonpCallback: callback, success: function () { console.log("Success"); }, error: function (err) { ...

Responsive Bar Chart using jQuery Mobile and ChartJS that appears on the screen only after resizing

I have been experimenting with adding a responsive bar chart using Chart.js in one of my JQM projects. Here is what I have accomplished so far: http://jsfiddle.net/mauriciorcruz/1pajh3zb/3/ The Chart needs to be displayed on Page Two and it should be res ...

Chaos in the IE8 drop-down menu situation

I'm having trouble with my code for a menu with drop-downs in ie8. It works fine in all other browsers but there seems to be an issue with the positioning of the main menu and submenus. Any thoughts on what might be causing this? <ul id="nav" clas ...

The jQuery UI Tab is failing to scroll within its container

Scenario : I am facing an issue with a scrollable container in IE8. The containing div is supposed to be scrollable and it holds my jquery UI tab div. Issue: While scrolling the container in IE8, other content within it also scrolls, but the jQuery UI t ...

PHP & Ajax integration for uploading files via a Bootstrap modal dialog

After exploring numerous posts for a solution to my issue without success, I felt the need to seek assistance specific to my problem here. Currently, I am using form elements in a modal. All the form elements successfully submit through my Ajax and store ...

Submitting a form and using Ajax to upload an image

Is there a way to submit an image file to php using ajax without assigning the file to a variable with onchange event? I've tried triggering the request on submit click, but keep getting the error message: "cannot read property 0 of undefined." <ht ...

Is there a way to execute a callback function once the page has finished loading through AJAX in

I'm in need of a way to attach new events and execute certain functions on a webpage that loads dynamically. Unfortunately, the resources I've found so far are outdated or lack necessary details (even the jqm docs). My current setup involves jQue ...

What could be causing my HTML email to appear distorted when viewed in Gmail?

After spending the last 6 hours tackling this issue, I am still stuck. Despite my extensive googling, it seems that divs are supposed to work in emails post-2017. All the online HTML email testers I have used indicate that my email layout is perfectly fine ...

Switching the class from "untoggled" items to the selected item in React

Recently, I developed a toggle component known as Accordion. Within this component, I am iterating through an array of objects and displaying them in the following format: {object.map((o) => ( <Accordion key={o.id} title={o.question} className="i ...

Retrieve information from a row by clicking on it, then present it in a dialog box for editing

I'm working on a project using Angularjs to retrieve data from a table row when it's clicked and then display the data in an editable dialog box. I need help with implementing this feature. Here is my current progress: <table> ...

Tips for effectively utilizing CSS classes with current-device

Attempting to lock the screen orientation in portrait mode for mobile and iPad devices using a CSS snippet found on css-tricks.com. @media screen and (min-width: 320px) and (max-width: 767px) and (orientation: landscape) { html { transform: rotate ...

How can I find the hexadecimal color value from this jQuery plugin?

Hey everyone, I've been using this awesome jQuery plugin for a color picker. However, I'm curious about where the hex value of the chosen color is stored when the user selects it. I need to process it and save it to the database. Thanks! If you& ...

"Is there a way to retain the value of a variable outside of an ajax success

I've been working on some form validation functions and here's what I've come up with: <script> $(document).ready(function() { var dataObject = {}; $('#username').keyup(function () { id = $(this).attr('id'); ...

Retrieve Vue.js component property within an Ajax function

When dealing with a component function that fetches data from an URL and attempts to set that data to a property, there seems to be an issue where this is not accessible from the AJAX closure scope. var MyComp = Vue.extend({ props: { html_pro ...