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

What's the Method to get an element by its class?

What method do I use to get an element by its class?

For an ID we would do:

document.getElementById("")

so what do we do when it's a class we are after?

Cheers

2 Answers

Personally I just use JQuery :)

But for Javascript you could use the following...

document.getElementsByClassName(classname);

wow beat me to it!

*blows off smoking gun barrel and puts gun back in holster* :)

Thanks I knew it was somewhere around there. Calvin Nix what do you use in Jquery?

$(".classname").whateverGoesNext

?

Yeah that is all you have to do. It makes life a lot easier.

Try document.getElementByClassName and let me know if that works!