Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKamronbek Ibatov
5,300 PointsWhen the program runs, my h1 heading disappears.
When the program runs, the h1 heading I wrote in the html file disappears and only the paragraph with the message is left. How can I make the h1 heading stay above the paragraph?
1 Answer
jb30
44,806 PointsUsing document.write()
clears the previous html code. To keep your header, avoid using document.write()
or include your header as part of the input for document.write()
.
To add a paragraph with a given message after the first <h1>
element of the page, you could try
let messageParagraph = document.createElement("p");
let messageText = document.createTextNode("Your message here");
messageParagraph.appendChild(messageText);
let headerHeading = document.querySelector("h1");
headerHeading.insertAdjacentElement("afterend", messageParagraph);
where you create a paragraph, add text to it, and insert it after your <h1>
element.
If security is not a concern and you want to add the paragraph to the end of the document's body, you could use document.body.innerHTML += "<p>Your message here</p>";
instead.