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

document.getElementsByClassName('header').style.backgroundColor = 'red';

document.getElementsByClassName('header').style.backgroundColor = 'red';

I went to the Firefox console trying to change the background color of the Treehouse.com header.

The tag is header and it's class is also header.

I used this code but I keep getting "type error: style is undefined".

1 Answer

When you use the method getElementsByClassName you are requesting an array of items. style is a property against a single element. I would suggest trying to reference the element you wish by first selecting it by it's index in the array you are given.

I.E.

document.getElementsByClassName('header')[0].style.backgroundColor = 'red'