mrdaliri
mrdaliri

Reputation: 7328

append an element to another one and append all of them to another element

I want to create a div and append one ul (with a class) and one h5 (with text) to it. then, append that div to another element.

I think this code should do it:

$("div").addClass( "nice" )
    .append( $("ul").addClass("myclass") )
    .append( $("h5").text("heading") )
    .appendTo( $("#another_div") );

but it doesn't work and browser crashes!

How? (i know i can use $("div").html(), but i don't like it!)

Thank you.

Upvotes: 1

Views: 229

Answers (2)

user800014
user800014

Reputation:

Live example: http://jsfiddle.net/kzLmp/

$(function(){
    $("<div>").addClass( "nice" )
    .append( $("<ul>").addClass("myclass") )
    .append( $("<h5>").text("heading") )
    .appendTo( $("#another_div") );
});

Upvotes: 2

John Kalberer
John Kalberer

Reputation: 5790

Your problem is that by using "div" and "ul" as your selectors, jQuery is searching the dom instead of creating elements. Try this:

$("<div></div>").addClass( "nice" )
    .append( $("<ul></ul>").addClass("myclass") )
    .append( $("<h5></h5>").text("heading") )
    .appendTo( $("#another_div") );

Upvotes: 2

Related Questions