javascript - Add css attribute to element -


i want add css attributes element. when code down here loose attributes had impact on element.

function checknr(id) {     var value = document.getelementbyid(id).value;     if (parsefloat(value) == nan) {         document.getelementbyid(id).setattribute("style", "border:2px solid red; background-color: rgb(255, 125, 115);");     }     else {         document.getelementbyid(id).setattribute("style", "border:default; background-color: rgb(255, 255, 255);");     }  } 

before use method have attributes:

float: left; width: 50px; 

and afterward got specific attributes javascript method. so, want add attributes not replace them.

setting style attribute that, overwrites attribute , removes set styles.

what should set styles directly instead changing style property :

function checknr(id) {     var elem  = document.getelementbyid(id),         value = elem.value;     if (parsefloat(value) == nan) {         elem.style.border = '2px solid red';          elem.style.backgroundcolor = 'rgb(255, 125, 115)');     } else {         elem.style.border = 'none';          elem.style.backgroundcolor = 'rgb(255, 255, 255)');     } } 

Comments

Popular posts from this blog

php - get table cell data from and place a copy in another table -

javascript - Mootools wait with Fx.Morph start -

php - Navigate throught databse rows -