tags:

views:

33755

answers:

6

Hi, How to create a div element in JQuery??

+2  A: 
<p id="foo"></p>

$('#foo').html('<div></div>');
egaga
+8  A: 
$("<div></div>").appendTo("div#main");

will append a blank div to <div id="main"></div>

Caius
+4  A: 

Hope this helps

div = $("<div>").html("Loading......");
$("body").prepend(div);
Ish Kumar
+17  A: 

First select the parent element with something like

$("#id"), $("<element>") or $(".class")

then use the .append("<div>foo</div>") function. Alternatively, you can use the .html() as mentioned above.

$("#foo").append("<div>hello world</div>")
cm2
+13  A: 

Technically $('<div></div>') will 'create' a div element (or more specifically a DIV DOM element) but wont add it to your HTML document. You will then need to use that in combination with the other answers to actually do anything useful with it (such as using the append() method or such like).

The manipulation documentation gives you all the various options on how to add new elements: http://docs.jquery.com/Manipulation

samjudson
how do you give your new div a new id? and say the id has to be dynamic? i.e '<div id=' + myNewId +'></div>'
towps
use the .attr() method.
samjudson