Home > UI/UX Development > HTML > How to Create HTML Element using JavaScript?

How to Create HTML Element using JavaScript?

This article provides information about creating HTML Elements dynamically instead of static HTML Elements using JavaScript.

You can create HTML Element dynamically using document.createElement() method. It takes parameter as tag Name.

Syntax

In createElement() method it takes first parameter as tagName or nodeName to create Element.

An optional ElementCreationOptions object, In createElement() method it takes parameter as options this is optional parameter.

I am assuming you have an HTML File with html and body tags.

Add script tag at the bottom of body tag in html File, in script tag you have to write below code.   

The above piece of code is created blank div element dynamically. 

Now, you have to append the created HTML element to the HTML document using appendChild() method.

You can attach created element to the document using the below piece of code.

Output

Open the console in the network tab, you can highlight the div element. 

If you like this article, please share and comment.

This Article is TAGGED in , , , . BOOKMARK THE permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">