css - How can use JavaScript to set a style with !important? -


this question has answer here:

i have no problems except when trying add !important. not take. need add in order override font size dynamically.

here how adjusting style:

function newfont(newsizea) {     var elems = document.getelementsbyclassname('a');     for(i=0; i<elems.length; i++) {         elems[i].style.fontsize = newsizea + 'px!important';     }  }  newfont(20); 

with !important fails, without !important works.

edit: needs work ie8 hoping there cross browser solution knows of.

your css won't parse !important declaration when written way (it's expecting legal values fontsize in value space, means numbers, not numbers , string). instead, can write way, setting style attribute.

this should supported ie8 , up, according quirksmode , msdn.

function newfont(newsizea) {      var elems = document.getelementsbyclassname('a');      for(i = 0; < elems.length; i++) {          elems[i].setattribute('style', 'font-size:' + newsizea + 'px' + '!important');      }    }    newfont(20);
<div class="a">asdf</div>


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -