Create an Element and set its Text content using JavaScript

avatar

Borislav Hadzhiev

Fri Jan 07 20222 min read

banner

Photo by Tim Marshall

Create an Element and set its Text content using JavaScript #

To create an element and set its text content:

  1. Use the document.createElement() method to create the element.
  2. Use the textContent property to set the element's text content.
  3. Add the element to the page using the appendChild() method.

Here is the HTML for the examples in this article.

index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <div id="box"></div> <script src="index.js"></script> </body> </html>

And here is the related JavaScript code.

index.js
// โœ… Create element const el = document.createElement('div'); // โœ… Add text content to element el.textContent = 'Hello world'; // โœ… Or set the innerHTML of the element // el.innerHTML = `<span>Hello world</span>`; // โœ… (Optionally) Set Attributes on Element el.setAttribute('title', 'my-title'); // โœ… (Optionally) Set styles on Element // el.style.backgroundColor = 'salmon'; // el.style.color = 'white'; // โœ… add element to DOM const box = document.getElementById('box'); box.appendChild(el);

We used the document.createElement method to create the element.

The only parameter we passed to the method is the type of element to be created (div in the example).

The createElement method returns the newly created element.

You can use the textContent property to set the element's text content or the innerHTML property to set the element's inner HTML markup.

Here is an example that sets the inner HTML of the element.

index.js
const el = document.createElement('div'); el.innerHTML = ` <span style="background-color: salmon; color: white;"> Hello world </span> `; // โœ… (Optionally) Set Attributes on Element el.setAttribute('title', 'my-title'); // โœ… (Optionally) Set styles on Element // el.style.backgroundColor = 'salmon'; // el.style.color = 'white'; // โœ… add element to DOM const box = document.getElementById('box'); box.appendChild(el);

I used backticks `` (not single quotes) to wrap the HTML string, because backticks allow us to easily create a multi-line string.

You shouldn't use the innerHTML property with user provided data without escaping it. This would leave your application open to cross-site scripting attacks.

If you need to set attributes on the element, use the setAttribute method.

The setAttribute method takes 2 parameters:

  1. name - the name of the attribute whose value is to be set.
  2. value - the value to assign to the attribute.

In the example, we set the value of the element's title attribute to my-title.

index.js
const el = document.createElement('div'); el.innerHTML = ` <span style="background-color: salmon; color: white;"> Hello world </span> `; el.setAttribute('title', 'my-title');
If the attribute already exists, the value is updated, otherwise a new attribute is added with the specified name and value.

You can set styles on the element by setting properties on its style object.

index.js
const el = document.createElement('div'); el.innerHTML = ` <span> Hello world </span> `; el.style.backgroundColor = 'salmon'; el.style.color = 'white';

Note that multi-word properties are camel-cased when using the style object.

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee