jared jared - 1 month ago 9
CSS Question

Add a class to a DIV with javascript

Example HTML:

<div id="foo" class="class_one"></div>


How can I add the class
class_two
without replacing
class_one
?

end result:

<div id="foo" class="class_one class_two"></div>

Answer

Standard javascript:

document.getElementById('foo').className += ' class_two'

or JQuery:

$('#foo').addClass('class_two');
Comments