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

I want to insert some text using JavaScript to blank HTML document, but using red color.

Hey guys! I want to insert some text using JavaScript to blank HTML document, but using red color. How can I do that? Thanks for further responds!

2 Answers

Steven Parker
Steven Parker
243,318 Points

This is a pretty simple task. Which courses have you taken so far?

But here's one simple example:

document.write("<h1 style='color:red'>Sample Text</h1>");

Thanks! I've taken JavaScript Basic, DOM courses (also HTML, CSS, C#). Was a little bit confused and got some errors in compiler, couldn't figure them out. A lot of information is given in those courses, sometimes it is hard to memorize it all and to use it exactly when I need. So I practice :)

You could use innerHTML() to place an element with a class that sets the color of the element to red.

document.getElementsByTagName("body")[0].innerHTML = '<p class="red">Test</p>';
.red {
    color: red;
}

thanks!