prepopulate radio button

Discussion in 'JavaScript' started by tableman, Sep 28, 2006.

  1. #1
    This script prepopulates the select box, state, but not the radio button, tobacco.

    
    <script language="javascript" type="text/javascript">
    
    function prepop()
    {
    document.long_form.state.value="<?php echo $_SESSION['state']; ?>";
    document.long_form.tobacco.value="<?php echo $_SESSION['tobacco']; ?>";
    }
    window.onload=prepop;
    
    </script>
    
    Code (markup):
    The value for tobacco, yes or no, shows on the page with view source, so the value got to the page, but I can't find how to address a radio button so the delivered value is preselected, and I'm out of ideas.

    Any suggestions?
     
    tableman, Sep 28, 2006 IP
  2. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #2
    i believe it is .selectedIndex is what you need.
     
    ccoonen, Sep 28, 2006 IP
  3. tableman

    tableman Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for your interest, ccoonen. selectedIndex is used with arrays in drop down boxes.

    I now have the answer.

    Here is the solution for prepopulating a radio button that receives its value from a php session (substitute input code accordingly for any other source):

    
    var tob; 
    var tabac="<?php echo $_SESSION['tobacco']; ?>"; 
    {
    if (tabac=="yes") 
    {tob="0"} 
    
    else if (tabac=="no")
    {tob="1"} 
    
    document.long_form.tobacco[tob].checked = true;
    
    Code (markup):
    This line is then not needed for the radio button:

    
    document.long_form.tobacco.value="<?php echo $_SESSION['tobacco']; ?>";
    
    Code (markup):
    Now the text boxes (don't need javascript), drop down boxes, AND radio buttons are all prepopulated.

    Resolved!
     
    tableman, Sep 29, 2006 IP