jQuery
jQuery Create DIV Examples (4 Easy Ways)
Did you always wonder how to use jQuery to create div? In this article, I am going to show you 4 ways of creating div using jQuery.
Examples of using jQuery to Create DIV
Let’s assume that you have a div with ID “destination”. Let’s see how you can create a div element in this “destination” div:
Example 1: By using .html() method in jQuery:
//Use: html() - Overwrites any existing content of #destination and replaces it with custom text
$("#destination").html("<div>This is some custom text</div>");Example 2 – By using .append() method in jQuery:
//Use: append() - Adds the custom text at the end of the existing text in #destination
$("#destination").append("<div>This is some custom text</div>");Example 3 – By using .appendTo() method in jQuery:
//Use: appendTo() - Adds the custom text at the end of the existing text in #destination
$("<div>This is some custom text</div>").appendTo("#destination");Example 4 – By using .prependTo() method in jQuery:
//Use: prependTo() - Adds the custom text at the beginning of the existing text in #destination
$("<div>This is some custom text</div>").prependTo("#destination");Very easy, isn’t it?
Do you know of any other ways to create div using jQuery? Feel free to share by commenting below.