Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a class to a DIV with javascript

Tags:

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> 
like image 429
jay Avatar asked Jul 12 '12 03:07

jay


1 Answers

See deekshith's answer below if you only need to support new browsers

Standard javascript:

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

or JQuery:

$('#foo').addClass('class_two'); 
like image 83
Josh Mc Avatar answered Oct 07 '22 11:10

Josh Mc