JavaScript Changing HTML
A ferry slip already on the page names the boat Gull, the time 09:10, and the note Boarding. Changing HTML means writing new content into an element you already hold. boat.textContent = "Heron" stores the plain words Heron. note.textContent = "<em>Late</em>" shows those characters, and note.querySelector("em") is null. note.innerHTML = "<em>Late</em>" builds an em whose text is Late. After that, note.textContent is Late, while note.innerHTML still contains the tags. A second write, note.innerHTML = "Dock 2", deletes the em. note.insertAdjacentHTML("beforeend", " <span>gate B</span>") keeps Dock 2 and adds gate B, so the note reads Dock 2 gate B. Nothing assigns the time, so it stays 09:10.
Writing, not another search
The previous page compared ways to find a node. This page does not count a collection, does not read an index, and does not walk parentElement. getElementById only hands you the slip. The lesson is the value you assign.
What each write leaves on the note
The note starts as Boarding. Read the rows in order. Each assignment replaces or extends that note.
📊 What each write leaves on the note
| Write | What you see | What remains | Easy to mix up |
|---|---|---|---|
textContent = "Heron" on the boat |
Heron | one text node | an element whose name is Heron |
textContent = "<em>Late</em>" |
the characters <em>Late</em> |
no em element |
a real em |
innerHTML = "<em>Late</em>" |
Late | one em |
textContent still holding the tags |
innerHTML = "Dock 2" |
Dock 2 | the em is gone |
the em sitting beside Dock 2 |
insertAdjacentHTML("beforeend", " <span>gate B</span>") |
Dock 2 gate B | Dock 2 plus a span |
innerHTML, which would drop Dock 2 |
textContent stores a string and does not parse tags. innerHTML parses tags and drops every child the element already had. insertAdjacentHTML parses tags as well, but beforeend places the new nodes after the current content. A boat name typed at the window belongs in textContent. The same string in innerHTML would treat a less-than sign as markup.
Read the slip before you write
The file opens with three values. No assignment has run.
console.log(document.getElementById("boat").textContent);
console.log(document.getElementById("when").textContent);
console.log(document.getElementById("note").textContent);
The logs are Gull, 09:10, and Boarding. The time is its own paragraph. Later writes never touch when.
Plain words stay words
textContent replaces every child with one text node. Characters that look like a tag stay characters.
let boat = document.getElementById("boat");
let note = document.getElementById("note");
boat.textContent = "Heron";
note.textContent = "<em>Late</em>";
console.log(boat.textContent);
console.log(note.textContent);
console.log(note.querySelector("em"));
The first log is Heron. The second is the characters <em>Late</em>. The third is null. The note is showing a tag as text. It did not build an element.
innerHTML builds the element
Give those same characters to innerHTML. The parser turns the string into nodes and replaces whatever the note held.
let note = document.getElementById("note");
note.innerHTML = "<em>Late</em>";
console.log(note.textContent);
console.log(note.innerHTML);
console.log(note.querySelector("em") === null);
The first log is Late. The second is <em>Late</em>, because innerHTML returns the markup, tags included. The third is false: the em is there, so it is not null. The visible words and the markup string are different readings of one element.
The next innerHTML clears the em
A new innerHTML value replaces the whole note. The em does not remain beside the new text.
let note = document.getElementById("note");
note.innerHTML = "Dock 2";
console.log(note.textContent);
console.log(note.querySelector("em"));
The first log is Dock 2. The second is null. Boarding was already gone. The em goes away on this write.
Add gate B and keep Dock 2
insertAdjacentHTML("beforeend", ...) parses a fragment and puts it at the end. This string starts with a space, so the phrases stay two words. Other positions exist. This page uses beforeend only.
let note = document.getElementById("note");
note.insertAdjacentHTML("beforeend", " <span>gate B</span>");
console.log(note.textContent);
console.log(note.querySelector("span").textContent);
The first log is Dock 2 gate B. The second is gate B. Dock 2 is still in the note. Assigning that same span string with innerHTML would have deleted Dock 2.
One line under the slip
The script is at the end of the body, after the slip exists. It saves the word Late before Dock 2 replaces the em, then writes one line from the boat, that saved word, and the note after gate B is added.
<!DOCTYPE html>
<html>
<head>
<title>changing html — page</title>
</head>
<body>
<article id="slip">
<h2 id="boat">Gull</h2>
<p id="when">09:10</p>
<p id="note">Boarding</p>
<p id="line">…</p>
</article>
<script>
let boat = document.getElementById("boat");
let note = document.getElementById("note");
boat.textContent = "Heron";
note.innerHTML = "<em>Late</em>";
let late = note.textContent;
note.innerHTML = "Dock 2";
note.insertAdjacentHTML("beforeend", " <span>gate B</span>");
document.getElementById("line").textContent = boat.textContent + " / " + late + " / " + note.textContent;
</script>
</body>
</html>
The line reads Heron / Late / Dock 2 gate B. The time still reads 09:10. Late on the line is the word saved before the wipe. The note itself no longer says Late. The next page changes CSS on elements like these. It does not introduce another way to pick them.
Check the slip
- Log the three starting texts and expect Gull, 09:10, and Boarding
- Set the boat to Heron and the note to the em characters, then expect that character string and a null em
- Set innerHTML to the em string, then expect textContent Late, innerHTML still carrying the tags, and an em that is not null
- Set innerHTML to Dock 2 and expect the em to be null
- Add the span with beforeend and expect Dock 2 gate B, then the span text gate B
- Read the line and expect Heron / Late / Dock 2 gate B, and read the time and expect 09:10
Misses on this slip
The page still opens. The assignment wrote the wrong kind of value.
- Putting a name typed at the window into
innerHTML, where a<becomes markup - Expecting
textContent = "<em>Late</em>"to create anem - Reading
textContentafter the em write and expecting the tags in that string - Assigning
innerHTMLagain and expecting theemto remain - Adding gate B with
innerHTMLand losing Dock 2 - Dropping the leading space, so the note reads
Dock 2gate B - Setting
stylehere; the next page changes CSS - Calling
createElementhere; a later page builds new nodes
Leave with this
textContentwrites plain text and does not parse tagsinnerHTMLparses tags and replaces every child- After the em write,
textContentis Late andinnerHTMLstill contains the tags - A second
innerHTMLdeletes theem insertAdjacentHTML("beforeend", ...)adds markup and keeps Dock 2- The time stays 09:10 because nothing assigns it
- The line reads Heron / Late / Dock 2 gate B
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top