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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:43:00+00:00 2026-06-15T03:43:00+00:00

I have an unexpected problem. HTML <div id=div1 class=myDiv></div> <div id=div2 class=myDiv></div> <div id=div3

  • 0

I have an unexpected problem.

HTML

<div id="div1" class="myDiv"></div>
<div id="div2" class="myDiv"></div>
<div id="div3" class="myDiv"></div>
<div id="div5" class="myDiv"></div>
<div id="div6" class="myDiv"></div>

JS

$(function() {
    var $divs = $('.myDiv');
    // create new div not in tree
    var $div = $('<div/>').attr("id","div4").addClass('myDiv');
    // insert #div4 in right position. Only in stack, not in dom tree.
    $divs = $divs.slice(0,3).add($div).add($divs.slice(3));

    console.log($divs);
});

output

[div#div1.myDiv, div#div2.myDiv, div#div3.myDiv, div#div5.myDiv, div#div6.myDiv, div#div4.myDiv]

Warning: I DON’T want to insert it in dom tree (like $div.appendBefore($divs[3])), I just want append it in my stack $divs.

I thought that this stack was an ordered list. So, my goal was to create on fly a #div4 and insert it in $divs stack without insert it in DOM tree. The insert works but it seems that jquery ignore the order given.

I’ve other solutions for this problem (i.e. append in dom with a display none), ok.. but:

My question is: why? It’s a bug for some cache optimization, or it’s a documented feature?

I’ve also tried:

var $newDivs = $();
$divs.each(function(i,e) {
    if(i==3) 
        $newDivs = $newDivs.add($div);
    $newDivs = $newDivs.add(e);
});
console.log($newDivs);

but the output is the same.

EDIT: Just for completeness: this was just a fast hack for a complex code. I know that the purpose is not clean

  • 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-15T03:43:01+00:00Added an answer on June 15, 2026 at 3:43 am

    That’s not the purpose of a jQuery object. KEep your data-structures straight; I’m not sure how the concept of a ‘stack’ came into this at all. Use the regular built in JS arrays (which keep items ordered):

    $(function() {
        // make an array of existing divs
        var divs = $('.myDiv').toArray();
    
        // create new div
        var newDiv = $('<div/>').attr("id","div4").addClass('myDiv').get(0);
    
        // insert newDiv at index 4 in array
        divs.splice(4, 0, newDiv);
    
        // Rejoice.
        console.log(divs);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with this jQuery code: $(document).ready( function(){ alert($('#search').val()); var dummyInput =
I have a model dialog. I overrided OnCancel function to stop unexpected exiting by
I have a "simple" 4 class example that reliably shows unexpected behavior from java
I am getting some unexpected behavior from Html.EditorFor(). I have this controller: [HandleError] public
I have the problem when solr put some unexpected value in field. in schema.xml
I have this in my background.html: chrome.management.onEnabled.addListener(function(ExtensionInfo info) { alert('123'); }); which gives me
I have a part of html that is incompletely structured. Example: <div id='notrequired'> <div>
I have an unexpected ; error on this line: $action = ($sessionMinus == $_SESSION['initial_count'])
While transforming some data to SQL Azure, I have noted an unexpected behavior. I
I have a small script and it throws Unexpected token error on line 2

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.