sader Posted November 17, 2009 Share Posted November 17, 2009 Hi I try to create elements with javascript like so: /*this is not actual code but the point is in the sequence*/ var elem = document.createElement("div"); elem.setAttribute("class", "nice_div"); var border_width = elem.currentStyle.borderWidth; elem.width = 250 - border_width; documrent.body.appendChild(elem); Ok now the problem. As you can see I am trying get borderWidth after I have set class for element but here's the thing currentStyle is not updated - all values(currentStyle.color, currentStyle.background etc.) are null so I can't calculate and set correct width for element. I was able to eliminate this issue by changing sequence of some actions: var elem = document.createElement("div"); documrent.body.appendChild(elem); //<= first append elem.setAttribute("class", "nice_div"); //set class after the node is appended var border_width = elem.currentStyle.borderWidth; //now currentStyle values are updated elem.width = 250 - border_width; So ok I found the way to make my code to work, but I am not sure I like that I must remember the order maybe there is another better way to solve this problem? for example function elem.update() elem.refersh() or smth.. ? Anyway in FF getComputedStyle works well in both examples (order is ignored) Link to comment https://forums.phpfreaks.com/topic/181904-currentstyle-issue/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.