Reputation: 311
I have created a new DIV via JQuery. Now I want to take an existing div and put it into my dynamically created div. My current solution is not working.
$(".body").append("<div id='new'></div>", function(){
$("#old").appendTo("#new");
});
Am I not able to set non dynamic divs to be children of dynamic divs?
Thanks
Upvotes: 0
Views: 2424
Reputation: 3202
Firstly are you intentionally using .body or you want to append to just
Secondly you are using id="#new" where it should be just id="new"
Third, read .append() documentation on how to use it.
Fourth, you'll need .html()
now try this.
$("body").append("<div id='new'></div>");
$("#new").append($('#old'));
Upvotes: 0
Reputation: 57095
You are using .append( content [, content ] )
$(".body").append("<div id='new'></div>", function(){
$("#old").appendTo( $("#new") );
});
so content gets append to no function is called. See Fiddle Demo
You can use .append( function(index, html) )
$(".body").append(function () {
return $("<div/>", {
id: "new"
}).append($("#old"));
});
Upvotes: 0
Reputation: 34416
You're not referencing the 'new' div properly. I suggest removing the hash from the actual id and then doing this:
$("body").append("<div id='new'></div>");
$("#old").appendTo( $("#new") );
You can leave the hash there if you'd like, but, unless you're using HTML5, id's cannot begin with special characters.
EDIT: You shouldn't have to use the callback and you need to eliminate the '.' from the 'body' as it is not a class unless you have created one. http://jsfiddle.net/ZDFg4/
Upvotes: 0
Reputation: 201
Try this
$("<div/>", { id: "new" }).appendTo("body");
$("#old").appendTo("#new");
Upvotes: 1