jQuery document.createElement equivalent?

In this tutorial, let's see how can we create HTML elements using jQuery which can be done by document.createElement in JavaScript?
Submitted by Pratishtha Saxena, on June 17, 2022

JavaScript method document.createElement() creates a new HTML tag. An HTML tag has to be specified within the brackets. Some text can be passed in the tag using document.createTextNode(). It is then append into the other tags or if not then the body tag of the HTML document using element.appendChild(). This is how createElement() method of JavaScript works.

Now in jQuery, different methods can be used to create DOM elements. Methods like append(), prepend(), after(), before() are used to do the same. As the name suggests, append() method will add the content at the end of the selected HTML element tag, prepend() will add the content at the beginning of the selected tag, after() will add content after the selected element and before() will add content before the selected element in DOM.



Note: Always remember to apply the CDN link while working with jQuery.

<script src=""></script>


HTML Code:

<!DOCTYPE html>
<html lang="en">
      <meta charset="UTF-8">
      <script src=""></script>
      <div id="main">

jQuery Function:

      var h1 = $('<h1>Hello World</h1>')
      h1.attr('style','text-align: center; color: blue')


Example: document.createElement equivalent


Comments and Discussions!


Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.