April 29, 2009

Setting an HTML element’s class with Javascript

I was pulling my hair out trying to figure out why I was getting a parse-error with this code… DUH!

document.getElementById('test').class = "test"

“class” is a reserved name, of course… So you access it with:

document.getElementById('test').className = "test"