Code has been added to clipboard!

innerHTML

Reading time 3 min
Published Sep 8, 2017
Updated Oct 10, 2019

innerHTML JavaScript: Main Tips

  • This property is used to set or return the HTML content inside an element.
  • Using innerHTML, you can change HTML content inside element tags as you like, but not the tags themselves.

innerHTML Property Explained

So, what is innerHTML? Let's start by reminding everyone that every HTML element has an inner HTML property. It is used to define HTML tags and also the content that is inserted between them. This property is one of the components of the DOM that lets developers manipulate the appearances of web pages. In other words, it allows you to get information about all DOM elements.

Additionally, you have the opportunity to modify or replace HTML elements. Therefore, you should learn how the innerHTML JavaScript property can be used to your advantage.

People can struggle and complain about innerHTML not working. Such things usually occur because of human error, when strings are not appropriately defined, or there are some mistakes in JavaScript code.

This property is used to set or return the HTML content inside an element:

Example
document.getElementById("samplePara").innerHTML = "Help! This paragraph has changed!";

Correct Syntax

To avoid mistakes, we suggest you learn the proper way of manipulating DOM elements using the innerHTML JavaScript property. After you learn the basics, you won't have to worry about innerHTML not working ever again.

To return the HTML content inside an element, you should follow this code example:

HTMLElementObject.innerHTML

If you wish to modify HTML elements with the innerHTML JavaScript property, we present you the appropriate way of setting the HTML content inside an element:

HTMLElementObject.innerHTML = text

Parameters

As you can see in the syntax examples, the innerHTML JavaScript property can only take one value. It is called text.

Text specifies the HTML content inside an element. As a parameter, it is used to make modifications to the text of the element. After applying it, you will be able to change texts that are positioned between different HTML tags.

Return Value

After the innerHTML JavaScript property is applied to a DOM element, you will receive its content. A string which represents the HTML content inside an element is called the return value.

There are no limitations to the type of tags the inner HTML can set and return. Therefore, feel free to practice. The more you try, the more you learn!

Code Examples

To help you understand what is innerHTML property and how it's used, we'll provide you with useful code examples that you can practice your code-writing skills on. After you click the Try it Live button, you will be able to make modifications and learn without having to leave the browser!

The first example displays how you can make modifications to two DOM elements:

Example
document.getElementById("samplePara").innerHTML = "Hello, sir, what seems to be the problem?";   
document.getElementById("sampleDiv").innerHTML = "It's the paragraph. It changed!";

The following code is used to alert the content of <p> with id="samplePara":

Example
alert(document.getElementById("samplePara").innerHTML);

You can also remove the HTML content from the <p> tag with the following code:

Example
document.getElementById("samplePara").innerHTML = "";

The final code example shows the code for when you want to modify the content of HTML element, its URL, and the target of a link:

Example
document.getElementById("sampleAnchor").innerHTML = "BitDegree";  
document.getElementById("sampleAnchor").href = "https://www.BitDegree.org";  
document.getElementById("sampleAnchor").target = "_blank";
What Is JavaScript Used For?
Tutorial
Introduction
Output
Syntax
Comment
Commands
Operators
Comparison and Logical Operators
Data Types
Math.random()
Type Conversion
Function Definitions
Events
Objects
Object Properties
Prototype
Array
Sorting Arrays
Strings
Numbers
Number Format
Math Object
Onclick Event
Date
Date Formats
Scope
Regular Expressions
Reserved Words
Common Mistakes
Performance
Forms
Form Validation
Window: The Browser Object Model
Popup Boxes
Cookies
JSON
AJAX Introduction
AJAX Form
Automatic File Download
Functions
Array Methods
String Methods
Date Methods
Timing Events
Cheat Sheet
JavaScript in HTML
HTML DOM Methods
HTML DOM Changing HTML
HTML DOM Animation
HTML DOM EventListener
HTML DOM Navigation
HTML DOM NodeList
HTML DOM Element Nodes
Array Functions
Boolean
Calling a Function
Date Functions
Global Objects
Input Text
Operator
Statements
String Functions
Math
Math.random
Number
RegEx
alert
array.filter
array.length
array.map
array.reduce
array.push
array.sort
break and continue
className
confirm
decodeURIComponent
for
forEach
if
indexOf
innerHTML
location.reload
number.toString
onclick
onload
parseInt
prompt
replace
setAttribute
setInterval
setTimeout
slice
splice
string.includes
string.indexOf
string.split
style.display
submit
substr
substring
switch
test
throw, try and catch
toLowerCase
toUpperCase
use strict
while
window.history
window.location
window.navigator
window.screen