jquery - Add replace the class of a div upon some conditions with Javascript -


i trying replace class of div javascript. have looked @ posts did not seem (e.g. change element's class javascript). have straightforward innerhtml

document.getelementbyid("colored-title").innerhtml = "add comments"; 

html straightforward , works when there no condition on class

<div id="colored-title"></div> 

i tried many options (i listed them below) none of them seems work.

if (array[current][5] == 4) {      document.getelementbyid("colored-title").addclass("green-text");     document.getelementbyid("colored-title").classname= "green-text";     document.getelementbyid("colored-title").classname+= "green-text";     document.getelementbyid("colored-title").setattribute=("class","green-text"); } else {     // other format } document.getelementbyid("colored-title").innerhtml = "add comments"; 

you can add classname throught javascript .classname='classname' should work.check if condition see why not working

window.onload = function() {    document.getelementbyid("colored-title").classname= "green-text";      }
.green-text {    color: green;  }
<div id="colored-title">    hello  </div>

hope helps


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -