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 trial

JavaScript JavaScript and the DOM (Retiring) Making Changes to the DOM Style and Element Creation

How would you create a new h1 element with JavaScript? document. (" "); Submit Answer

How would you create a new h1 element with JavaScript?

document. (" "); Submit Answer

3 Answers

George Raymond
George Raymond
3,087 Points

document.createElement("h1");

<!DOCTYPE html>
<html>
<body>

<p>Click the button to create a h1 element with some text.</p>

<button onclick="myFunction()">Click to create</button>

<script>
function myFunction() {
    var myStyle = document.createElement("H1");
    var myText = document.createTextNode(prompt("This is an example of text written in H1");
    h.appendChild(myText);
    document.body.appendChild(myStyle);
}
</script>

</body>
</html>

This code shows an example of how to use document.createElement