Search⌘ K
AI Features

Adding a New Element

Understand how to dynamically add new elements to a web page by creating elements, setting their properties, and inserting them into the DOM using JavaScript methods like createElement and appendChild.

Adding a new element to a web page can be broken into three steps:

  • Create the new element
  • Set element properties
  • Insert the new element in the DOM

For example, suppose you want to add the language “Python” to the list of languages on our page.

Let’s study each of these steps.

Creating the element

You’d create an element using the createElement() method. This method is used on the document object and takes the tag of the new element as a parameter. This method also returns the element created as an object (here stored in a variable called pythonElement).