Sunday, 15 September 2013

Float right on clicking the right button and float left clicking the left button using a single function and only javascript? -



Float right on clicking the right button and float left clicking the left button using a single function and only javascript? -

this tried:

--html--

<div id="box1"> <button type="button" onclick="float()" value="1" id="left">left</button> <button type="button" onclick="float()" value="2" id="right">right</button> </div>

--css--

#box1 { width:250px; height:490px; display:block; float:left; }

--js--

function float() { var r = document.getelementbyid("right").value, l = document.getelementbyid("left").value if(r==2){ document.getelementbyid("box1").style.cssfloat="right"; } if(l==1){ document.getelementbyid("box1").style.cssfloat="left"; }

}

i tried removing 2nd if statement , code worked fine, i.e., float changed right. please help. in advance.

try this:

function float() { var r = document.getelementbyid("right").value; var l = document.getelementbyid("left").value; if(r==2){ document.getelementbyid("box1").style.cssfloat="right"; } if(l==1){ document.getelementbyid("box1").style.cssfloat="left"; }

javascript

No comments:

Post a Comment