Possible Duplicate:
How do I add a class to a given element?
I'd like to have JavaScript add a class to my h1 elements automatically.
How would I add JavaScript to replace the h1 element with h1="classs" etc?
Possible Duplicate:
How do I add a class to a given element?
I'd like to have JavaScript add a class to my h1 elements automatically.
How would I add JavaScript to replace the h1 element with h1="classs" etc?
Try this:
var h1 = document.getElementsByTagName("h1");
for (i = 0; i < h1.length; i++) {
h1[i].className += ' classs';
}
getElementsByTagName returns a NodeList, not a single element. You have to iterate over the list.Using Jquery it's simple. Loop all of the h1 elements adding the classname to it
$(function(){
$('h1').each(function(){
$(this).addClass('classname');
});
});
Or if you want to add a class per specific element ID
$(function(){
$('#eleID').addClass('classname');
});
$('h1').addClass('class')