text field and select field dependence

Discussion in 'JavaScript' started by asherinho, Jul 31, 2008.

  1. #1
    Hi guys! I want to create a form whereby whenever a value is selected from a list of select field it's corresponding value should appear on the text field.
    example,if the select field has a list of file numbers,then if a certain file number is selected the corresponding file name should apppear on the text field
     
    asherinho, Jul 31, 2008 IP
  2. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Let us know if you have any experience in javascript.
     
    rohan_shenoy, Jul 31, 2008 IP
  3. asherinho

    asherinho Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I am a begginer to javascript
     
    asherinho, Jul 31, 2008 IP
  4. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #4
    Before you get your hand on the script, i suggest you read the following few references:

    The javascript:
    
    <script type="text/javascript">
    function set_text_field()
    {
    var select_box=document.getElementById("select_box");
    var selectionOptionIndex=select_box.selectedIndex;
    var optionSet=select_box.getElementsByTagName("option");
    var selectedOption=optionSet[selectionOptionIndex];
    var value_to_copy=selectedOption.value;
    document.getElementById("text_field").value=value_to_copy;
    }
    </script>
    
    Code (javascript):
    the HTML:
    
    <select onchange="set_text_field()" id="select_box">
    <option value="1">File 1</option>
    <option value="2">File 2</option>
    <option value="3">File 3</option>
    <option value="4">File 4</option>
    <option value="5">File 5</option>
    </select>
    <input type="text" id="text_field">
    
    HTML:
     
    rohan_shenoy, Jul 31, 2008 IP