Wednesday, January 9, 2013

Assigning CSS float values in javascript

I have to give credit for this information to an anonymous poster on stack overflow:  http://stackoverflow.com/questions/606470/is-there-a-cross-browser-way-of-setting-style-float-in-javascript

While creating and adding styles to some DOM elements with javascript, I noticed that my IDE was unhappy with me using this syntax:

domElement.style.float = "right";

The trouble is that 'float' is a reserved keyword in javascript, and the above syntax will cause unexpected results which vary across browsers.  I learned that I should be using: domElement.style.styleFloat = "right"; but this was still not working Firefox.

The cross browser solution ( Chrome, IE, Firefox ) :
domElement.style.styleFloat = "right";
domElement.style.cssFloat = "right";

Now everyone is happy.

No comments:

Post a Comment