Common DOM Interactions

Once you have an element, you change its text with textContent, its HTML with innerHTML, its look with style, and its CSS classes with classList.

Apprentice · JavaScript · Updated

By

After this lesson you can

  • Change an element's text with textContent, or its HTML with innerHTML
  • Change an element's look with the style property and with classList add, remove and toggle

Key takeaways

  • textContent sets an element's text exactly as written, so a tag in the string shows as plain text.
  • innerHTML reads the string as HTML, so tags like strong become real elements. Never give it text a user typed.
  • element.style sets one inline style. A CSS name with a hyphen becomes camelCase, so background-color is backgroundColor.
  • classList.add, remove and toggle change one CSS class and leave the element's other classes alone.

Once you've found an element, these are the changes you'll make most often: its text, its HTML, its inline styles and its CSS classes. Each example finds the element with the id target first.

Changing Text

Change the text of a paragraph.

const element = document.getElementById("target");
element.textContent = "New random quote of the day";

textContent shows the string exactly as written. If it holds a tag like <strong>, the page shows the tag as text instead of making the words bold.

Add HTML

Replace the contents of an element with new HTML.

const element = document.getElementById("target");
element.innerHTML = "<strong>New bold text</strong>";

innerHTML reads the string as HTML, so <strong> becomes a real element. That's why you never give innerHTML text a user typed: their text would run as HTML on your page. For text, use textContent.

Change Inline Styles

Add or change inline styles while the page is open.

const element = document.getElementById("target");
element.style.color = "maroon";
element.style.backgroundColor = "khaki";

A CSS property with a hyphen, like background-color, is written in camelCase in JavaScript: backgroundColor. A hyphen would read as a minus sign.

Add/Remove CSS Classes

Add or remove CSS classes while the page is open.

const element = document.getElementById("target");

// add a new class
element.classList.add("highlight");

// remove a class
element.classList.remove("hidden");

// toggle adding/removing a class
element.classList.toggle("active");

Each of these changes only the class you name, and leaves the element's other classes alone. toggle adds the class when the element doesn't have it, and removes it when it does.

Do it: show today's quote of the day

The study group's page has a Quote of the day box. It waits, hidden, with placeholder words, until the script fills it in each morning. Today's quote is in. The script has found the three elements already; make the changes:

  • The quote's paragraph says Small steps every day add up.
  • The author's line says by Maya Torres, with her name in bold. The line is HTML: by <strong>Maya Torres</strong>.
  • The box gets a khaki background, for today only.
  • Show the box: it has the class hidden, which hides it. Take that class away, and leave its card class alone.

Practiced live on the lesson page: write the JavaScript right there and check it.

Check yourself

2 questions to try on the lesson page.

So what

Text, styles and classes are how a page reacts to the person using it, and a class from your stylesheet is usually the cleanest way to change how something looks.

Key terms

Cheat sheets