Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Arrays Loop Through Arrays Search for a Value in an Array

Kamronbek Ibatov
Kamronbek Ibatov
5,295 Points

When 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

Using 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");

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.