JavaScript

innerHTML property in JavaScript

The innerHTML property enables the manipulation of the HTML content in various ways. The innerHTML allows the user to set/update the HTML content, or get the content of an HTML element. The innerHTML property uses the getElementById method to identify the element and perform a relevant operation.

For better understanding, we have divided the article into various chunks to demonstrate the usage of the innerHTML property in detail:

  • How innerHTML property works in JavaScript
  • How to get the content of an element using the innerHTML property
  • How to set the content of an element using innerHTML property
  • How to update the content of an element using innerHTML property

How innerHTML property works in JavaScript

The innerHTML property sets/updates or returns the HTML content of an element. Both functionalities of an innerHTML property have different syntaxes that are provided below.

To get the HTML content:

HTMLObject.innerHTML;

To set/update the HTML content:

HTMLObject.innerHTML="new-val";

In the above syntaxes, the HTMLObject instance identifies the HTML element on which the innerHTML will be applied. And the element is identified by using the getElementById method. Whereas the new-val is any value that would be set to an HTML element.

How to use innerHTML property in JavaScript

This section enlists various examples that demonstrate the usage of the innerHTML property in JavaScript.

How to use innerHTML property to update HTML content

The following code snippets demonstrate the working of innerHTML property to update the HTML content.

HTML

<h4 id="h"> Welcome to Linuxhint </h4>

<button onclick="get()"> click here to get </button>

The above code has an h4 element with id=”h” and a button with onclick attribute.

JavaScript

function get(){

alert(document.getElementById("h").innerHTML);

}

The above JavaScript code creates a get() function and contains an alert that would show the content of element id=”h“.

Output

The output shows that the content of an HTML element is displayed as an alert content.

How to use innerHTML property to update HTML content

In this section, you will learn to update the content of an HTML element using innerHTML property.

HTML

<p id="p1"> LinuxHint </p>

<button onclick="update()"> click here to update </button>

The above code creates a paragraph having id=”p1” and a button with an onclick attribute.

JavaScript

function update(){

document.getElementById("p1").innerHTML=" Welcome! innerHTML property is being used! ";

}

The JS code written above creates a function named update() which will be invoked on the click of the button to apply innerHTML property on element(paragraph) id=”p1“.

Text Description automatically generated with medium confidence

Output

From the output, it is observed that the content of the paragraph is being updated.

How to to use innerHTML property to set the HTML content

Here, we will show how innerHTML sets the content of an HTML element. For this, the HTML and JavaScript code practiced in this section are elaborated below.

HTML

<p id="p1"> This is a Paragraph </p>

<button onclick="set()"> click here to set the content </button>

<div> <p id="p2"> </p> </div>

The above HTML code has two paragraphs where the value of the first paragraph(id=”p1“) will be used to set the value of second paragraph(id=”p2“).

Note: The div in the above code is just for sake of understating.

JavaScript

function set(){

const old = document.getElementById("p1").innerHTML;

document.getElementById("p2").innerHTML= old;

}

A set() function is created that has two statements. The first statement gets the content from paragraph(id=”p1“) and saves it in a variable(old). And the second statement sets the value of the variable to the paragraph (id=”p2“).

Text Description automatically generated

Output

From the above output, the paragraph has nothing to show but upon clicking the button the content of the paragraph(id=”p1“) is set to the paragraph(id=”p2“)

Conclusion

The innerHTML property of JavaScript allows to get or update/set the content of an HTML element. Using the getElementById method, the innerHTML property identifies an HTML element. In this article, the innerHTML property of JavaScript is demonstrated in a brief manner. Each subsection here presents the functionality of innerHTML property to get, update, or set the content of an HTML element.

About the author

Adnan Shabbir