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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:39:17+00:00 2026-06-10T10:39:17+00:00

I am trying to loop through a series of elements, iterating a number and

  • 0

I am trying to loop through a series of elements, iterating a number and applying it in different ways to pairs of two.

So, say I have twenty elements, and want to leave the first 6 elements untouched, I slice at 6. Then, I need to apply styles to each of those elements starting at #7, but need to do so in pairs of two. So, elements 7 & 8 will be top: 0; left: 0; while elements 9 & 10 will be top: 240px; left: 240px;

Elements 11 & 12 will then be top: 480px; left: 0; while elements 13 & 14 will be top: 720px; left: 240px;

I hope that pattern makes sense.

I do not know the best way to achieve this. My code is below, however, I only refer to the elements using ‘this’ and in no way am trying to identify pairs. That is what I do not understand how to do.

Here is my example:

$('#main article').slice(6).each(function(i) {

    // first pair of two
    $(this).css({
        top  : i * 240 + 'px'
    });

    // second pair of two
    $(this).css({
        top  : i * 240 + 'px',
        left : 480 + 'px'
    });

});

Any help is really appreciated. Thanks,

  • 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-10T10:39:18+00:00Added an answer on June 10, 2026 at 10:39 am

    CSS only non-incremental update below original JS answer.

    You could change it to use a for loop on the list and increment the index partway through:

    var el = $('#main .article');
    for(var i=0; i <= el.length; i++){
        var left = (i == 0) ? 0 : i * 40;
            $(el[i]).css({
                top: i * 40 + "px",
                left: left + "px"
            });
            i++;
            $(el[i]).css({
                top: i * 40 + "px",
                left: left + "px"
            });
    }​
    

    Of course you can still use the slice, but you’ll need to position those first 6 elements correctly if you are using position:relative or absolute so that they don’t overlap each other.

    Here’s a fiddle showing it working.

    Edited from comment discussion below:

    If the incremental values aren’t important to you you can achieve the pattern with straight CSS:

    .article:nth-child(4n-1),
    .article:nth-child(4n-1) + li{
        opacity: 0.4;
    }
    

    Here’s the fiddle for that.

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

Sidebar

Related Questions

I'm trying to loop through a number of items, and create a json object.
I am trying to loop through a vectors elements and trying to find out
I am trying to recursively loop through a series of directories (about 3 levels
So I am trying to loop through some elements and change some text based
I am trying to loop through a number of results from a form, but
I am trying to loop through two columns in a DataGridView and add them
I'm trying to loop through a list of results from MySQL and display all
I'm trying to loop through objects from my database in the template and one
I'm trying to loop through all files of .properties extension, in a root folder:C:\ExecutionSDKTest_10.2.2\,
I am trying to loop through a directory of text files and combine them

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.