Advertisement
Advertisement


Creating a div element in jQuery


Question

How do I create a div element in jQuery?

2016/01/22
1
1556
1/22/2016 8:19:03 PM

Accepted Answer

You can use append (to add at last position of parent) or prepend (to add at fist position of parent):

$('#parent').append('<div>hello</div>');    
// or
$('<div>hello</div>').appendTo('#parent');

Alternatively, you can use the .html() or .add() as mentioned in a different answer.

2017/12/06
1092
12/6/2017 5:04:39 PM


Technically $('<div></div>') will 'create' a div element (or more specifically a DIV DOM element) but won't 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.

2019/04/25

d = document.createElement('div');
$(d).addClass(classname)
    .html(text)
    .appendTo($("#myDiv")) //main div
.click(function () {
    $(this).remove();
})
    .hide()
    .slideToggle(300)
    .delay(2500)
    .slideToggle(300)
    .queue(function () {
    $(this).remove();
});
2015/09/29

div = $("<div>").html("Loading......");
$("body").prepend(div);    
2013/09/05

$("<div/>").appendTo("div#main");

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

2017/07/11

A short way of creating div is

var customDiv = $("<div/>");

Now the custom div can be appended to any other div.

2017/07/11

Source: https://stackoverflow.com/questions/867916
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]