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?
Facebook comments:
13 Responses to Use JavaScript to change “float” style
Leave a Reply Cancel reply
Archives
- May 2012
- November 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- May 2011
- March 2011
- February 2011
- January 2011
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- January 2010
- December 2009
- October 2009
- August 2009
- July 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- June 2007
Tags
.net 2008 asp.net asp.net mvc asus eee c# corona cpa doctrine dom eee eee pc Internet Marketing ios iPad iphone iPhone development iphone sdk javascript make money netbook online marketing pagerank passion php playstation playstation 3 ps3 ranking ror ruby on rails self help Self Improvement self motivation seo sql success symfony take action the secret ultra compact laptop ultra compact notebook vb.net web development wordpress





Under IE6 you must use this:
obj.style.styleFloat=’left’;
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.
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.
Thanks man, very helpful! Much appreciated
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.
float is a reserved word in about every programming language, so it’s not silly.
Thanks a lot.. very help ful to me..
So that makes him plain stupid.
Thanks for posting this!
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
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>
Thanks a million, this solved my problem
Thank you! So simple, yet so much frustration.