Blog
Know me more?

Use JavaScript to change “float” style

Hey friends,

In a few days ago, I’ve just stuck with how to change “float” style in JavaScript and very confuse why it doesn’t work at all!

I’m using …

var obj = document.getElementById(“some1”);

obj.style.float = “left”;

No luck at all. I tried both on Internet Explorer and Firefox. Even I use Internet Explorer Toolbar and Firebug. I can change the style via that tools but no luck with JavaScript.

Silly?

I’m fool with DOM. I try to find a solution and suddenly found that.

In DOM, there are no “float” property but “cssFloat”.

Just change my code to …

var obj = document.getElementById(“some1”);

obj.style.cssFloat = “left”;

Really silly huh?

Seree -

8 Comments


  1. erwinke
    Nov 07, 2008

    Under IE6 you must use this:

    obj.style.styleFloat=’left’;


  2. Henrik
    Nov 27, 2008

    Ohh thx!
    i figured that it needed something in front, since float really is a datatype, so using it in obj.style.float would be impossible.


  3. Me
    Mar 21, 2009

    Bless you for posting this! I was tearing my hair out trying to figure out why my float code worked in FF but not IE!! And styleFloat works like a dream.


  4. Nathan
    Nov 26, 2009

    Thanks man, very helpful! Much appreciated


  5. Boss Hawk
    Dec 31, 2009

    Here is a little workaround for this problem:

    obj.setAttribute(“style”, obj.getAttribute(“style”) + “; float:left; “);

    The leading semicolon must be set because the IE sometimes changes the styles and always misses out the last semicolon.


  6. Eino
    Feb 19, 2010

    float is a reserved word in about every programming language, so it’s not silly.


  7. Bobby
    Feb 26, 2010

    Thanks a lot.. very help ful to me..


  8. Leon
    Mar 14, 2010

    So that makes him plain stupid.

Leave a Reply