I know that to replace one style, the code looks something like this:
myDOMElement.style.height = '400px';
But what if I want to completely replace the entire style object with one fell swoop, thereby speeding up work and avoiding redrawing? For example, I would like to do this:
//Get the computed style var computedStyle = window.getComputedStyle(myDOMElement); //Change some stuff in that CSSStyleDeclaration without rendering computedStyle.height = '10px'; computedStyle.width = '20px'; computedStyle.whatever = 'something'; //Apply the entirety of computedStyle to the DOM Element, thereby only redrawing once myDOMElement.style = computedStyle;
However, when I run this code, my new style is simply ignored. What can I do to fix this?
javascript dom css
Alexz
source share