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

  • SEARCH
  • Home
  • 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 9234929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:54:20+00:00 2026-06-18T06:54:20+00:00

I have 28 divs, all floated left with a width & height of 200px

  • 0

I have 28 divs, all floated left with a width & height of 200px with 5px margins on the right and bottom of each div. I have successfully figured out how to place other divs after other divs via jquery. I did it like so.

$( '.inner:nth-child(10)' ).after( '<div class="test">');

This works great! But what I would like to do is change the “inner” div classes nth-child position based on browser width and I’ve managed to get it working, somewhat.

Here is the code I’m using:

var $window = $(window);

function checkWidth() {
    var windowsize = $window.width();
    if (windowsize > 440) {
     $( '.inner:nth-child(10)' ).after( '<div class="test">');
    }
}

// Execute on load
checkWidth();
// Bind event listener
$(window).resize(checkWidth);

So I’m telling my browser to display the div “inner” after the 10th “test” div when the viewers browser width exceeds 440px. Again, this works, somewhat. It successfully displays the “inner” div in the correct position when the browser width exceeds 440px, and it also doesn’t show the div when the browser width is below 440px. But the huge problem is that whenever the window is resized, a whole bunch of divs start to be created. This is very puzzling to me. Here is the jsFiddle to demonstrate my issue:

http://jsfiddle.net/EUqEm/2/

You will see that at a glance it appears to be working fine but if you resize the HTML window within jsfiddle, a bunch of divs start to be created. In actuality, the div should always remain in the correct spot, right after the 10th inner div. Instead, it just creates a bunch of other divs when the window is resized.

If anyone could help me out with a solution to this problem and get this little issue working properly within jsfiddle, it would mean a ton to me! 🙂

  • 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-18T06:54:21+00:00Added an answer on June 18, 2026 at 6:54 am

    Can you try this?

        var $window = $(window);
    var resized=false;
    function checkWidth() {
    
        var windowsize = $window.width();
        if (windowsize > 440) {
            //if the window is greater than 440px wide then place the "inner" div after the 10th "test" div
            if(resized==false){
         $( '.inner:nth-child(10)' ).after( '<div class="test">');
                resized=true;
            }
        }
         if (windowsize <= 440) {
             $('.test').remove();
             resized=false;
         }
    }
    // Execute on load
    checkWidth();
    // Bind event listener
    $(window).resize(checkWidth);
    

    Is this what you wanted? In your version the resize function is calling .after() many times, adding a lot of new divs. It should work this way

    EDIT:
    With @pete s suggestion it’s easyer to add new colored divs.

        var $window = $(window);
    
    function checkWidth() {
        var windowsize = $window.width();
        if (windowsize > 440) {
            //if the window is greater than 440px wide then place the "inner" div after the 10th "test" div
            $('.test').remove();
         $( '.inner:nth-child(10)' ).after( '<div class="test">');
        }else{
            $('.test').remove();
        }
        if (windowsize > 500) {
            //if the window is greater than 440px wide then place the "inner" div after the 10th "test" div
            $('.test1').remove();
         $( '.inner:nth-child(12)' ).after( '<div class="test1">');
        }else{
             $('.test1').remove();
        }
    }
    // Execute on load
    checkWidth();
    // Bind event listener
    $(window).resize(checkWidth);
    

    You just have to add the css rule for the test1 class.

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

Sidebar

Related Questions

I have 3 divs all floated left. I want to set the second div
I have a fluid width container DIV. Within this I have 4 DIVs all
Hey there, I have 20 divs floated left with different height. I use this
Here's the idea: I have a div element, #content_wrapper, which encompasses three floated divs,
When I have three divs that all have float left I want the sections
I have three DIVs: <div style=float:left; id=a> a </div> <div style=float:left; id=b> b </div>
I have a two-column layout, composed of fixed-width floated divs. Inside the first column
I have 3 inner divs all floating left. They are wrrapped in a parent
I have div containing multiple divs and text. The inner divs are alternately floated
I have the following basic layout: <div id=sidebar style=float: left; width: 250px; display block;></div>

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.