Option values

Discussion in 'JavaScript' started by AdINo, Jul 5, 2007.

  1. #1
    Below is a option and a text box , well basicly what i wanted to do is whenever the user chooses a option , the value would be shown on the text box

    Wanted output :

    option = Women suit
    text box = 4

    Hope you guys could help me out.
    Thanx in advance

    /*
    <td width="128"><select size="1" name="cboItem1">
    <option selected>None</option>
    <option value="4">Women Suit</option>
    </select></td>

    <td width="71"><input type="text" name="txtItem1UnitPrice" size="7" value="0.00"></td>
    */
     
    AdINo, Jul 5, 2007 IP
  2. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #2
    You can use something like this:

    
    <html>
    <head>
      <script type="text/javascript">
    	function f_selected(p_index)
    	{
    	  var v = document.getElementsByTagName( "option" )[p_index];
    	  document.getElementById("idPrice").value = v.value;
    	}
    	</script>
    </head>
    <body>
      <select size="1" name="cboItem1" onchange="f_selected(this.selectedIndex)">
        <option value="" selected="yes">None</option>
        <option value="1.0">Women Suit</option>
        <option value="2.5">Men Suit</option>
        <option value="3.7">Men Hat</option>
      </select>
      <input id="idPrice" type="text" name="txtItem1UnitPrice" size="7" value=""/>
    </body>
    </html>
    
    HTML:
     
    ajsa52, Jul 5, 2007 IP
  3. nabil_kadimi

    nabil_kadimi Well-Known Member

    Messages:
    1,065
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    195
    #3
    AHHH too late...
    <form name="my_form">
    my_select: 
    <select name="my_select" onchange="window.document.my_form.my_text.value=this.value">
    	<option></option>
    	<option value="1">The 1st value</option>
    	<option value="2">The 2nd value</option>
    	<option value="3">The 3rd value</option>
    </select><br />
    my_input:
    <input type="text" name="my_text" />
    </form>
    HTML:
     
    nabil_kadimi, Jul 5, 2007 IP