Featured Article

JavaScript Set InnerHTML Example: Simple Code Snippets

Adam Jul 13, 2026

JavaScript's Document Object Model (DOM) manipulation is a fundamental aspect of web development. The innerHTML property is a key tool for modifying the HTML content inside an element. Let's dive into how to use it with practical examples.

HTML,CSS JAVASCRIPT cheatsheet
HTML,CSS JAVASCRIPT cheatsheet

JavaScript doesn't directly allowed to change HTML, but it allows manipulating the DOM through several properties and methods like innerHTML. This property provides the HTML of an element and can be used to change the HTML content of an element.

JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)

Using innerHTML to Change Text Content

innerHTML can be used to change the text content of an HTML element. Let's consider a simple example where we change the text of a <p> element.

setInterval in JavaScript
setInterval in JavaScript

First, let's create our HTML structure with an id for the p element:

```html

Hello, World!

⏳ Master JavaScript setTimeout(): Delay Code Execution Like a Pro! 💻✨  Pin Description:
⏳ Master JavaScript setTimeout(): Delay Code Execution Like a Pro! 💻✨ Pin Description:

```

Now, let's use JavaScript to change the text content:

```javascript document.getElementById('myPara').innerHTML = 'Hello, JavaScript!'; ```

Gotcha: innerHTML and Tags

The innerHTML property does treat its content as HTML, which can lead to unwanted results if you're not careful. For instance:

JavaScript Tips to Change Border Radius & Background Color
JavaScript Tips to Change Border Radius & Background Color

```javascript document.getElementById('myPara').innerHTML = 'Bold Text'; ```

Here, the <strong> tag will make the text bold, as expected. However, if you don't need a new HTML tag, consider using textContent instead:

```javascript document.getElementById('myPara').textContent = 'Bold Text'; ```

textContent simply sets the text content without interpreting it as HTML.

innerHTML and Inserting Elements

The JavaScript Cheat Sheet Every Beginner Wishes They Had
The JavaScript Cheat Sheet Every Beginner Wishes They Had

.innerHTML is also used to insert entire elements into the DOM. Let's create a new list item and add it to an <ol> element using innerHTML:

```javascript var newListItem = '

  • New List Item
  • '; document.getElementById('myList').innerHTML += newListItem; ```

    Here, the new <li> element is added to the end of the list. However, beware of this approach as it can lead to performance issues with large DOM changes.

    JavaScript Template Literals: Write Cleaner & Dynamic Strings 📝
    JavaScript Template Literals: Write Cleaner & Dynamic Strings 📝
    an info sheet with different types of web pages
    an info sheet with different types of web pages
    JavaScript Basics | Chapter 1 📖 | Part 1
    JavaScript Basics | Chapter 1 📖 | Part 1
    Modern Web Essentials Using JavaScript and HTML5
    Modern Web Essentials Using JavaScript and HTML5
    an image of a web page with different types of text
    an image of a web page with different types of text
    JavaScript Full Notes Every Beginner Must Save 💡
    JavaScript Full Notes Every Beginner Must Save 💡
    Javascript Loops & Iterations
    Javascript Loops & Iterations
    Update HTML in DOM: Change Text Live
    Update HTML in DOM: Change Text Live
    Javascript Fundamental
    Javascript Fundamental
    📘 JavaScript Basics| Linking JS File to HTML | Chapter 2 📖 Part 2
    📘 JavaScript Basics| Linking JS File to HTML | Chapter 2 📖 Part 2
    JavaScript Basics | Chapter 1 📖 | Part 2
    JavaScript Basics | Chapter 1 📖 | Part 2
    JavaScript Web Development Cheat Sheet: Your Essential Guide - Connect 4 Programming
    JavaScript Web Development Cheat Sheet: Your Essential Guide - Connect 4 Programming
    Javascript window.confirm() method | Javascript tutorial
    Javascript window.confirm() method | Javascript tutorial
    HTML Cheat Sheet 📄✨ Quick Guide for Beginners
    HTML Cheat Sheet 📄✨ Quick Guide for Beginners
    JavaScript Increment & Decrement Operators: Simplify Your Code 🔄
    JavaScript Increment & Decrement Operators: Simplify Your Code 🔄
    CSS Notes for Beginners (Easy + Quick Guide) 🎨
    CSS Notes for Beginners (Easy + Quick Guide) 🎨
    Create Image Accordion Slider With HTML CSS And JavaScript
    Create Image Accordion Slider With HTML CSS And JavaScript
    How To Print A Page Or Section in Javascript
    How To Print A Page Or Section in Javascript
    "Master JavaScript's Slice Method �: Easy Array & String Slicing Tips! 🚀 #CodingTips #WebDev"
    "Master JavaScript's Slice Method �: Easy Array & String Slicing Tips! 🚀 #CodingTips #WebDev"
    Java script string method
    Java script string method

    Alternatives to innerHTML

    While innerHTML offers flexibility, it's not the most efficient way to manipulate the DOM. Modern browsers offer more efficient alternatives like createElement, appendChild, or even DOM manipulation libraries like jQuery. However, understanding innerHTML is key to understanding how JavaScript interacts with the DOM.

    Exploring these aspects can significantly enhance your understanding and application of innerHTML in JavaScript. Happy coding, and remember, the web is always ready for your next line of code!