Skip to content
다시 돌아온학습 기록

Back to plain JavaScript: innerHTML vs innerText

1 min readLearningMoved from my Tistory blogRead the Korean original

In an interview at NHN (a Korean IT company), I saw that I couldn't build even basic JavaScript features. It was a big reality check, and I started reviewing JavaScript again.

list.js
Figure 1. list.js
A util function that creates DOM elements and an appendChild util function
Figure 2. A util function that creates DOM elements and an appendChild util function

Based on the elements I marked up, I built the DOM with util functions.

Working this way after a long break, I'm really, really realizing how valuable React is.

The difference between innerHTML and innerText

- innerText: In a structure like <div>Hello <span> World </span></div>, using innerText returns "Hello World".

- In a structure like <div>Hello <span> World </span></div>, using innerHTML returns the full HTML string, <span> World </span>. You can also change the HTML with innerHTML. For example, element.innerHTML = "<b> Bold text </b>" replaces the element's content with "Bold text".

One-line summary: innerText works with text and ignores HTML tags, while innerHTML works with HTML and handles strings that include the tags.

  • #innerHTML
  • #innerText
  • #vanilla JavaScript
  • #difference

More in Learning