Add css class using document.getElementsByClassName
<p>I am trying to add css class using javascript but its not working   </p>

<pre><code>var x = document.getElementsByClassName('oldclassname');
var i;
for (i = 0; i &lt; x.length; i++) {
  x[i].className += 'newclassname';
}
</code></pre>

<p>but when I tried changing background it works</p>

<pre><code>var x = document.getElementsByClassName("oldclassname");
var i;
for (i = 0; i &lt; x.length; i++) {

  x[i].style.backgroundColor = "red";
}
</code></pre>

<p>Am I doing anything wrong while adding css file</p>