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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:15:17+00:00 2026-06-18T19:15:17+00:00

I’ve been working on this jquery system where multiple div’s nth-child position changes or

  • 0

I’ve been working on this jquery system where multiple div’s nth-child position changes or “shifts” based on browser width. It’s almost complete but I’ve ran into one issue that I can’t manage to wrap my head around at this point, I’ll explain..

I have 4 divs by default with their own nth-child positions, then when your browser width or window size changes into one of my specified ranges, the divs nth-child position changes. I’m using the .empty technique to make this all work, and that is where my problem lies. So say I have four divs all with text within them.. when the browser width changes, the text inside the divs dissapears or “empties”. And when I do not use empty, the text within these divs stays, but the nth-child shifter doesn’t work correctly… here is the jsFiddle without empty.

http://jsfiddle.net/H5REk/8/
When you resize the HTML window the text colors red, blue etc. from the other divs still remain.. so I fix this by using .empty. like so:

http://jsfiddle.net/H5REk/7/

But again, when .empty is used and you’re not in the specified window size / browser width.. the text within the divs dissapear or get “emptied”.

So, the problem with one method is all my text within the divs get emptied. And then with the other method, divs or text is being recreated and not removed.. So how would I get it to work so the browser width nth-child changer works correctly while also making the text within my divs still stay?

  • 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-18T19:15:18+00:00Added an answer on June 18, 2026 at 7:15 pm

    Based on your comments I have a couple different solutions.

    I did not know whether you wanted the special colored boxes to temporarily replace the existing ones (current behavior) or if you wanted them to be appended after the nth-child box you specified (based on your comments).

    In both of these the removeSpecialBoxes method is called at the beginning of checkWidth

    Replacement Method (jsfiddle)

    This approach is a bit more complicated as you need to keep track of the items you remove and then reinsert the same exact item later. In addition to the below JS, there were some CSS changes as well, namely I made the special boxes have IDs and each have the class test.

    First, I created a new object to hold the stored boxes.

    var theStored = {};
    

    Then I created a couple methods to handle the creation and removal of special boxes.

    function makeSpecialBox(id, content) {
        // Create a new special box with the class name and the content
        var specialBox = $("<div />", {
            "id": id,
            "class": "test"
        }).html(content);
        // Return the special box
        return specialBox;
    }
    
    function removeSpecialBoxes() {
        // Get the special boxes
        var specialBoxes = $("#wrapper div").filter(".test");
        // For each special box
        specialBoxes.each(function(index, specialBox) {
            // Get the special box's ID
            var specialBoxId = $(specialBox).attr("id");
            // Get the associated stored box
            var storedBox = theStored[specialBoxId];
            // Replace the special box with the stored box
            $(specialBox).replaceWith(storedBox);
        });
    }
    

    Then I modified your setNthPosition method to handle the storing and replacing of the existing box with the special box.

    function setNthPosition(theDiv, newPos){
        // Generate a new special box
        var specialBox = theDivs["div"+theDiv].clone();
        // Get the special box ID
        var specialBoxId = specialBox.attr("id");
        // Get the existing box to replace
        var existingBox = $("#wrapper div:nth-child("+newPos+")");
        // Clone the existing box and store it based on the special box ID
        theStored[specialBoxId] = existingBox.clone();
        // Replace the existing box with the special box
        existingBox.replaceWith(specialBox);
    }
    

    Appending Method (jsfiddle)

    This approach is a bit simpler. It is essentially the existing code from your question with a few minor changes.

    function makeSpecialBox(className, content) {
        // Create a new special box with the class name and the content
        var specialBox = $("<div />").addClass(className).html(content);
        // Return the special box
        return specialBox;
    }
    
    function removeSpecialBoxes() {
        // Get the special boxes
        var specialBoxes = $("#wrapper div").filter("[class^='test']")
        // Remove the special boxes
        specialBoxes.remove();
    }
    
    function setNthPosition(theDiv,newPos){
        // Generate the special box
        var theClone=theDivs["div"+theDiv].clone();
        // Append the special box after the nth-child
        $("#wrapper div:nth-child("+newPos+")").after(theClone);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.