JavaScript AcademyProfessional Engineering

Learning object 39 of 100

DOM update visualizer

Change text, HTML, classes, attributes and styles on a live preview and compare what each kind of update does.

Loading…

What it shows

textContent sets plain text and is safe for any data. innerHTML parses its string as HTML, which is powerful but dangerous with user input. classList adds, removes and toggles classes that CSS styles; setAttribute and getAttribute work with any attribute, including aria- and data-. Inline styles through element.style are best kept for values that are calculated at run time.

Guided experiment

  1. In textContent vs innerHTML, compare the two paragraphs. Which one created a b element?
  2. In Classes, run toggle twice. What is the final class list?
  3. In Attributes, find the attribute that screen readers use.

Predict

What does el.textContent = "<b>Hi</b>" display?

Related

Lessons: Lesson 53: Reading and changing content, Lesson 55: Updating styles, attributes and classes

Projects: Project 9: Shopping Cart Price Calculator

Source: web/assets/js/objects/object-039.js in the companion repository.