Since the question is about creating and appending multiple objects at the same time using jQuery, here is an approach:

$('body').append([
    $('<div/>',{ "class": "freeze" }),
    $('<div/>',{ "class": "parent" }).append([
        $('<div/>',{ "class": "loadimg" }),
        $('<div/>',{ "class": "header" }),
        $('<div/>',{ "class": "msg" })
    ]);
]);

In some cases operating with structural data is more reliable and more convenient than raw markup

Answer from aleha_84 on Stack Overflow
🌐
jQuery
api.jquery.com › append
.append() | jQuery API Documentation
Supported input includes DOM elements, jQuery objects, HTML strings, and arrays of DOM elements. For example, the following will insert two new <div>s and an existing <div> as the last three child nodes of the body: Since .append() can accept any number of additional arguments, the same result can be achieved by passing in the three <div>s as three separate arguments, like so: $('body').append( $newdiv1, newdiv2, existingdiv1 ).
🌐
W3Schools
w3schools.com › jquery › jquery_dom_add.asp
jQuery Add Elements
In the following example, we create several new elements. The elements are created with text/HTML, jQuery, and JavaScript/DOM. Then we append the new elements to the text with the append() method (this would have worked for prepend() too) :
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-combine-multiple-elements-and-append-the-result-into-a-div-using-javascript
How to Combine multiple elements and append the result into a div using JavaScript ? | GeeksforGeeks
February 28, 2020 - output: Approach 2: Appending elements using strings instead of nodes. Declare an empty JavaScript string to store HTML elements in that string. Keep adding new elements to a string.
🌐
Talkerscode
talkerscode.com › howto › jquery-append-multiple-elements.php
jQuery Append Multiple Elements - TalkersCode.com
In this article we will show you the solution of jQuery append multiple elements, here we needs to use click() and append() methods.
🌐
EyeHunts
tutorial.eyehunts.com › home › jquery append multiple elements
jQuery append multiple elements - Tutorial - By EyeHunts
May 22, 2023 - To create and append multiple elements using jQuery, you can use a combination of the $() function and the appendTo() method.
🌐
DEV Community
dev.to › dcodeyt › how-to-append-multiple-elements-at-once-in-javascript-dom-39eg
How to Append Multiple Elements at Once in JavaScript DOM - DEV Community
September 27, 2022 - It supports a total of 4 positions, but the one we're interested in for appending elements is beforeend (insert before the end of the parent element). Let's see it in action. /* HTML: <ul id="fruit"> <li>Apple</li> <li>Banana</li> </ul> */ const fruitList = document.getElementById("fruit"); // Add 3 new <li>'s to the list of fruits fruitList.insertAdjacentHTML("beforeend", ` <li>Pear</li> <li>Orange</li> <li>Grape</li> `); As you can see, it's easy to append multiple elements using plain HTML.
Find elsewhere
🌐
DEV Community
dev.to › dcodeyt › append-multiple-elements-using-append-302h
Append multiple elements using append() - DEV Community
August 17, 2020 - If you'd prefer this in the form of a video, check out my tutorial on the append() method here 👇 · The append method allows you to append multiple elements or text nodes to a parent node.
🌐
TutorialsPoint
tutorialspoint.com › how-to-combine-multiple-elements-and-append-the-result-into-a-div-using-javascript
How to combine multiple elements and append the result into a div using JavaScript?
We can use the append() method to append HTML to the particular element using JQuery. We can use the append() method multiple times to append multiple elements to a particular HTML element.
🌐
YouTube
youtube.com › watch
Append multiple elements at once using append() - JavaScript DOM Tutorial - YouTube
In this video I'll be showing you how to use the append() method in JavaScript which lets you append multiple nodes to a parent node (or element) in one go.T...
Published: August 15, 2019
🌐
Stack Overflow
stackoverflow.com › questions › 26047563 › jquery-append-multiple-elements
for loop - jQuery: append multiple elements - Stack Overflow
September 26, 2014 - Copyvar t = $(this); var counter = $("<ol class='slide-counter'></ol>"); if (n > 1) { t.append(counter); for (var i = 0; i < n; i++) { var dot = $("<li></li>"); counter.append(dot); } $(".slide-counter li:first-child").addClass("active"); } ... Save this answer. ... Show activity on this post. I think you need to turn dot into a string, otherwise you're trying to add a unique element to it more than once.
🌐
Stack Overflow
stackoverflow.com › questions › 56448091 › how-to-append-massively-multiple-elements-to-the-dom-using-jquery › 56448213
How to append (massively) multiple elements to the DOM using jQuery
June 4, 2019 - What I did to fix this is, instead of appending every row individually to the table, pushing each row element to an array, and after the array is complete, using the apply prototype function, extending the append jQuery function in order to apply the whole array at once.
🌐
jQuery
api.jquery.com › appendTo
.appendTo() | jQuery API Documentation
Append all spans to the element with the ID "foo" (Check append() documentation for more examples)
Top answer
1 of 6
2

Iterate over the divs and based on the number in id append the cloned span element. Use append() method with callback which iterates over the element and appends the callback return value.

// get all span with classname start with `section`
var $sec = $('span[class^=section]');

// iterate over div element and append the returned value
$('div.tab').append(function() {
  // generate the class name from id
  var cls = 'section' + this.id.match(/\d+$/)[0];
  // filter element from $sec
  return $sec.filter('span.' + cls)
    // clone the element
    .clone()
    // updaet the class to remove other class name
    // you can also use `prop('className',cls)`
    .attr('class', cls);
});

// remove the span elements from dom
$sec.remove();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="section2 section4">hello</span>
<span class="section1">World</span>

<div class="tab" id="tab1"></div>
<div class="tab" id="tab2"></div>
<div class="tab" id="tab3"></div>
<div class="tab" id="tab4"></div>
<div class="tab" id="tab5"></div>

UPDATE : If you want to maintain the other class or the span, then do something like.

// get all span with classname start with `section`
var $sec = $('span[class^=section]');

var $tab = $('div.tab');

// generate classnames based on tabs for removing later
var remove = $tab.map(function() {
  return 'section' + this.id.match(/\d+$/)[0];
}).get().join(' ');

// iterate over div element and append the returned value
$tab.append(function() {
  // generate the class name from id
  var cls = 'section' + this.id.match(/\d+$/)[0];
  // filter element from $sec
  return $sec.filter('span.' + cls)
    // clone the element
    .clone()
    // remove other class name
    .removeClass(remove).addClass(cls);
});

// remove the span elements from dom
$sec.remove();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="section2 section4">hello</span>
<span class="section1 section8">World</span>

<div class="tab" id="tab1"></div>
<div class="tab" id="tab2"></div>
<div class="tab" id="tab3"></div>
<div class="tab" id="tab4"></div>
<div class="tab" id="tab5"></div>

2 of 6
1

The problem in your code is you are just removing the original element and appending to similar id div and for section2 and section4 span are same so when script works for 4th div it removes from 2nd element and append to 4th.

You need to use .clone() and let the original span be at it place just clone it and append on as much div as you want and hide the initial span with css.

 $.each($(".tab"), function() {
   var id = $(this).attr('id').substring(3);

   if ($(".span_wrap .section" + id).length != 0) {
     $(".span_wrap  .section" + id).removeClass("section" + id).clone().appendTo($(this));
   }

 })
.span_wrap span {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="span_wrap">
  <span class="section2 section4">hello</span>
  <span class="section1">World</span>
</div>

<div class="tab" id="tab1"></div>
<div class="tab" id="tab2"></div>
<div class="tab" id="tab3"></div>
<div class="tab" id="tab4"></div>
<div class="tab" id="tab5"></div>

🌐
IONOS
ionos.com › digital guide › websites › web development › jquery append
How to insert elements into DOM tree with jQuery.append()
October 29, 2024 - jQuery.append() is a function from the jQuery library that adds content to the DOM (Document Object Model) tree dy­nam­i­cal­ly. You can use this method to append new elements or struc­tures to the end of a given object. The .append() method of jQuery has multiple options and can be used in con­junc­tion with other jQuery functions such as jQuery.re­move­Class() or jQuery.on().
🌐
GitHub
gist.github.com › nix1947 › 6c2c54f81ff42042655b5aba679bd792
jquery multiple elements appending · GitHub
jquery multiple elements appending. GitHub Gist: instantly share code, notes, and snippets.
🌐
TutorialsPoint
tutorialspoint.com › jquery › jquery-add-elements.htm
jQuery - Add Elements
The jQuery append() method adds the content at the end of the matched each element(s). You can also append multiple elements in a single function call.