In this section, we will learn what the document createElement() method is and how to use it in JavaScript.
What is document createElement() Method in JavaScript?
JavaScript document createElement() method is used to create a new HTML element in a document.
Basically, we set the name of the element as the argument of the method, and this element will be created afterward.
But you should know that using this method only creates a new element and we still need to use other methods like appendChild() to add this element to a document so that users can actually see it.
document createElement() Method Syntax:
doucment.createElement(element-name);
document createElement() Method Parameters
The method takes one argument, and that is the name of the element that we want to create.
For example, if we want to create a <p> element, then the argument of the method would be “p”.
document createElement() Method Return Value
The return value of this method will be a reference to the object that represents the newly created element.
Example: create element in JavaScript with createElement() method
See the Pen create element in JavaScript with createElement() method by Omid Dehghan (@odchan1) on CodePen.
Example: create div in JavaScript
See the Pen create div in JavaScript by Omid Dehghan (@odchan1) on CodePen.
Example: JavaScript create element with class
See the Pen JavaScript create element with class by Omid Dehghan (@odchan1) on CodePen.
Example: create a button in JavaScript
See the Pen create a button in JavaScript by Omid Dehghan (@odchan1) on CodePen.