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?

Tagged with:
 

Facebook comments:

13 Responses to Use JavaScript to change “float” style

  1. erwinke says:

    Under IE6 you must use this:

    obj.style.styleFloat=’left’;

  2. Henrik says:

    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 says:

    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 says:

    Thanks man, very helpful! Much appreciated

  5. Boss Hawk says:

    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 says:

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

  7. Bobby says:

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

  8. Leon says:

    So that makes him plain stupid.

  9. Penuel says:

    Thanks for posting this!

  10. Black_Diamond says:

    thanks a lot buddy..very helpful..Its the first time I am finding an answer in a forum..Using your information i created the following script which is is working in all browsers…Have a look

    function float1(){
    var aa = document.getElementById(‘yourdiv1′);
    var bb = document.getElementById(‘yourdiv2′);
    aa.style.cssFloat= ‘right’;
    aa.style.styleFloat= ‘right’;
    bb.style.cssFloat = ‘left’;
    bb.style.styleFloat = ‘left’;
    }
    function float2(){
    var aa = document.getElementById(‘yourdiv1′);
    var bb = document.getElementById(‘yourdiv2′);
    aa.style.cssFloat = ‘left’;
    bb.style.cssFloat = ‘right’;
    aa.style.styleFloat = ‘left’;
    bb.style.styleFloat= ‘right’;
    }

    aaaaaa
    bbbbbb

  11. Black_Diamond says:

    sorry I had a problem with copying it..Here it is the whole script thank you

    <script language='javascript' type='text/javascript'>
    function float1(){
    var aa = document.getElementById('yourdiv1');
    var bb = document.getElementById('yourdiv2');
    aa.style.cssFloat= 'right';
    aa.style.styleFloat= 'right';
    bb.style.cssFloat = 'left';
    bb.style.styleFloat = 'left';
    }
    function float2(){
    var aa = document.getElementById('yourdiv1');
    var bb = document.getElementById('yourdiv2');
    aa.style.cssFloat = 'left';
    bb.style.cssFloat = 'right';
    aa.style.styleFloat = 'left';
    bb.style.styleFloat= 'right';
    }
    </script>

    <input type="button" onclick="float1();" value="style 1">
    <input type="button" onclick="float2();" value="style 2">
    <br>
    <div id="yourdiv1">aaaaaa</div>
    <div id="yourdiv2">bbbbbb</div>

  12. Altin says:

    Thanks a million, this solved my problem ;)

  13. Thank you! So simple, yet so much frustration.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Looking for something?

Use the form below to search the site:


Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

reverse phone lookupTattoo DesignsSEO