Changing "float" onclick?

Discussion in 'JavaScript' started by jmansa, Aug 13, 2011.

  1. #1
    I'm trying to make a script which in realtime changes a DIV to float either left or right but somehow I'm doing something wrong?

    Here is what I got...
    function logoplace(that){
        document.getElementById('logoholder').style.float=that
    }
    Code (markup):
    <style type="text/css">
    div#logoholder {
        float:left;
        border:1px solid #666666;
    }
    </style>
     
    <input type="radio" name="logoplacement" value="left" onclick="logoplace(this)" />
    Left
    <input type="radio" name="logoplacement" value="right" onclick="logoplace(this)" />
    Right
    <br><br>
    <div id="logoholder">This is a test</div>
    HTML:
    What am I doing Wrong?
     
    jmansa, Aug 13, 2011 IP
  2. tiamak

    tiamak Active Member

    Messages:
    81
    Likes Received:
    2
    Best Answers:
    3
    Trophy Points:
    58
    #2
    use style.cssFloat or style.styleFloat (probably this one is corrent) instead of style.float ;)
     
    tiamak, Aug 15, 2011 IP
  3. rainborick

    rainborick Well-Known Member

    Messages:
    424
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    120
    #3
    No, style.float is correct. You're just not passing the setting to your function. Try:
    
    <input type="radio" name="logoplacement" value="left" onclick="logoplace('left')" />
    Left
    <input type="radio" name="logoplacement" value="right" onclick="logoplace('right')" />
    Right
    
    Code (markup):
    Using the explicit setting in the call to logoplace() eliminates the need to check the control's value or current setting, which may not be correct when the onclick() function is executed.
     
    rainborick, Aug 15, 2011 IP
  4. tiamak

    tiamak Active Member

    Messages:
    81
    Likes Received:
    2
    Best Answers:
    3
    Trophy Points:
    58
    #4
    @rainborick
    man style.float is (unfortunately) incorrect - u dont belive me? try it!

    btw below is working (tested!) code:
    cssFloat - works for firefox and chrome
    styleFloat - works for IE
    dont know about other browsers because i dont use them ;)
     
    tiamak, Aug 15, 2011 IP
  5. rainborick

    rainborick Well-Known Member

    Messages:
    424
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    120
    #5
    Sorry about that. You're right about the property name. But his code still needed the changes I suggested as well.
     
    rainborick, Aug 15, 2011 IP
  6. tiamak

    tiamak Active Member

    Messages:
    81
    Likes Received:
    2
    Best Answers:
    3
    Trophy Points:
    58
    #6
    u r right :)
    or this.value but yours is simplier and shorter so probably better ;)
     
    tiamak, Aug 15, 2011 IP