Javascript CSS

From Foochal

Jump to: navigation, search


How can I change the "class" of an element using javascript

Let's see an example where this code changes the class of the div from "foo" to "bar"

<div id="firstmessage" class="foo">Hello, world!</div>
<script>
 var e = document.getElementById("firstmessage");
 e.setAttribute("class", "bar");
 e.setAttribute("className", "bar"); // for IE which does not recognize "class"
 return;
}
</script>

Personal tools