Modern browsers solution
If you're only targeting modern browsers, then use element.classList.add
method to add a class:
element.classList.add("new-class");
And use element.classList.remove
method to remove a class:
element.classList.remove("new-class");
Old browsers solution
If you need to support Internet Explorer 9 or lower, then add a space plus the name of your new class to the className
property of the element. First, put an id
on the element so you can easily get a reference.
<div id="foo" class="some-class">
<img ... id="image1" name="image1" />
</div>
Then
var el = document.getElementById("foo");
el.className += " other-class";
Note the space before other-class
. It's important to include the space otherwise it compromises existing classes that come before it in the class list.
See also element.className on MDN.