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 OverflowSince 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
Using jQuery for most of this is complete overkill and just makes the code longer than necessary. Since everything you have is a constant, you can just create a single string of HTML and append that to the body.
If you want jQuery references to parts of it for later use, then just use .find() to find them later.
For example, you could just do this:
var html = '<div class="freeze"></div>' +
'<div class="parent">' +
'<div class="loadimg"></div>' +
'<div class="header"></div>' +
'<div class="msg"></div>' +
'</div>';
$(document.body).append(html);
For later references, you can do something like this:
var header = $(document.body).find(".header");
If you want to add the HTML as it is, then just use the jQuery append function. For example:
$('body').append('
<div id="test">\
<h1>This is the test</h1>\
<p>Hello, just a test</p>\
<a href="www.test.com">Click me</a>\
<a href="www.test.com">Click me again</a>\
</div>');
Change the selector from body to other DOM element/selector according to your requirement.
Or if you already have the div element with ID "test" in the document, then you can set the content using the html() function like below:
$("#test").html('<h1>This is the test</h1>\
<p>Hello, just a test</p>\
<a href="www.test.com">Click me</a>\
<a href="www.test.com">Click me again</a>');
$("#id_of_div").append($("#id_of_div_that_you_wanna_append").html());
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>
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>
You can flip it around using .appendTo() like this:
menu.appendTo("#horizontal_menu, #vertical_menu");
If menu isn't a jQuery object just wrap it:
$(menu).appendTo("#horizontal_menu, #vertical_menu");
Currently what's happening is it does get appended, or more accurately moved to #horizontal_menu, then gets immediately moved again. Passing a selector or multiple elements to .appendTo() makes it clone and append to each internally.
Does appending to one preclude you from appending that content to another ?
With respect to [jQuery wrapped] DOM elements, yes. Under the hood jQuery calls appendChild, which when called multiple times with the same element simply moves it from its current position to the new position.
You can try cloning to make a copy:
var $menu = $(menu);
$("#horizontal_menu").append($menu.clone());
$("#vertical_menu").append($menu.clone());
You can pass true to clone() if you want event handlers to be copied over as well.