The Wayback Machine - https://web.archive.org/web/20240602201429/https://www.geeksforgeeks.org/html-dom-innerhtml-property/
Open In App

HTML DOM innerHTML Property

Last Updated : 16 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The DOM innerHTML property is used to set or return the HTML content of an element.

Syntax:

It returns the innerHTML Property.

Object.innerHTML

It is used to set the innerHTML property.

Object.innerHTML = value

Where,

  • value: It represents the text content of the HTML element.

Return Value: This property returns a string that represents the HTML content of an element.

Example 1: This example shows how to change the content of the paragraph tag using the innerHTML property. 

HTML




<!DOCTYPE html>
<html>
 
<body style="text-align: center">
    <h1 style="color:green">
        GeeksforGeeks
    </h1>
    <h2>
        DOM innerHTML Property
    </h2>
    <p id="p">GeeksforGeeks</p>
 
 
    <button onclick="geek()">Click me!</button>
    <script>
    function geek() {
        document.getElementById("p").innerHTML =
        "A computer science portal for geeks.";
    }
    </script>
</body>
 
</html>


Output:

innerHTML Property

Example 2: This example shows how to get the value of the paragraph tag using the innerHTML property.

HTML




<!DOCTYPE html>
<html>
 
<body style="text-align: center">
    <h1 style="color:green">
        GeeksforGeeks
    </h1>
    <h2>
        DOM innerHTML Property
    </h2>
    <p id="P">A computer science portal for geeks.</p>
 
 
    <button onclick="geek()">Try it</button>
    <p id="p"></p>
 
 
     
    <script>
    function geek() {
        var x = document.getElementById("P").innerHTML;
        document.getElementById("p").innerHTML = x;
        document.getElementById("p").style.color = "green";
    }
    </script>
</body>
 
</html>


Output:

innerHTML Property

Supported Browsers: The browser supported by DOM innerHTML property are listed below:

  • Google Chrome 33.0 and above
  • Internet Explorer 4 and above
  • Microsoft Edge 12.0 and above
  • Firefox 1.0 and above
  • Opera 8.0 and above
  • Safari 9.0 and above


Previous Article
Next Article

Similar Reads

SVG Element.innerHTML Property
The SVG Element.innerHTML property returns innerHTML of the given element. Syntax: const content = element.innerHTML Return value: This property returns innerHTML of the element. Example 1: &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;body&gt; &lt;svg width=&quot;350&quot; height=&quot;100&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt; &lt;a href
1 min read
How to replace innerHTML of a div using jQuery?
For replacing innerHTML of a div with jquery, html() function is used. Syntax: $(selector).html() Example: &lt;!DOCTYPE html&gt; &lt;html lang=&quot;en&quot;&gt; &lt;head&gt; &lt;meta charset=&quot;UTF-8&quot;&gt; &lt;title&gt; changing-innerhtml-using-jquery &lt;/title&gt; &lt;link href= &quot;https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css
1 min read
Difference between innerText and innerHTML
innerText and innerHTML are both properties of JavaScript. However, there are differences in which the text is handled. Let us check the syntax of the two and then take an example to look at the differences. Syntax: Let us assume that we have a JavaScript variable called x. let x = document.getElementById('test');innerTextx.innerTextinnerHTMLx.inne
1 min read
How to add innerHTML to print page?
Here is the task to add innerHTML to the print page. Here we are going to change the present content inside the &lt;body&gt; element and sets them with the new specified content using document.body with the help of .innerHTML which specify the content to be printed. We’re going to discuss a few methods. Example 1: In this example, the specified con
2 min read
What is the Disadvantage of using innerHTML in JavaScript ?
The innerHTML property is a part of the Document Object Model (DOM) that is used to set or return the HTML content of an element. Where the return value represents the text content of the HTML element. It allows JavaScript code to manipulate a website being displayed. More specifically, it sets or returns the HTML content (the inner HTML) of an ele
3 min read
Difference between textContent and innerHTML
The textContent and innerHTML are properties of JavaScript. However, there are differences in the way the specified text is handled in JavaScript. Let us take a look at the syntax of both of these properties. Syntax: Let elem be a JavaScript variable that holds an element that is selected from the page. let elem = document.getElementById('test-btn'
2 min read
How to use innerHtml in React JS ?
React.js provides a easy way of handling UI updates. However, sometimes you need to inject HTML directly into your components. PrerequisitesIntroduction to ReactReact ComponentsReact HooksNPM or NPXIn this article, we are going to learn how can we use InnerHTML in React JS. We can add HTML into ReactJS by using the 'dangerouslySetInnerHTML' propert
2 min read
Why is it Better to use textContent instead of innerHTML ?
Using textContent instead of innerHTML is generally considered better practice when dealing with text content in the context of the Document Object Model (DOM). Here are a few reasons why textContent is often preferred: Security: Using textContent helps in reducing the risk of security vulnerabilities such as Cross-Site Scripting (XSS). When you us
1 min read
JavaScript innerHTML
The innerHTML property in JavScript is used to append the dynamic HTML or text content to an element using JavaScript. It is designed to add dynamic HTML, but developers also use it to add text content as well. It can be directly used with the element by selecting it using DOM manipulation. Syntax:selctedHTMLElement.innerHTML = "contentToAppend";Ex
2 min read
How to use innerHTML in JavaScript ?
The innerHTML property in JavaScript allows you to get or set the HTML content of an element as a string. Syntax ForGetting HTML content of an element:let element = document.getElementById("myElementId"); let htmlContent = element.innerHTML;Setting HTML content of an element:let element = document.getElementById("myElementId"); element.innerHTML =
2 min read
Article Tags :