Online Shopping : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to
Did You Find This Entry Useful?

20 of 27 people (74%) answered Yes
Recently 7 of 10 people (70%) answered Yes


How Can I Pass Value TextBox to OptionBox

May 14th, 2009 12:27
tikane tinjane, ha mo, Ole Clausen, thulasi ram,

Getting the text and value from two text inputs, you can add a new 
option at the end of a dropdown like this:
<script type="text/JavaScript">
function myTrans(f) {
  f.mySel.options[f.mySel.length] = new Option();
  f.mySel.options[f.mySel.length-1].text = f.txt.value;
  f.mySel.options[f.mySel.length-1].value = f.val.value;
<select name="mySel">
  <option value="0">first</option>
  <option value="1">second</option>
  <option value="2">third</option>
<input type="text" name="txt">
<input type="text" name="val">
<button onclick="myTrans(this.form)">transfer</button>
You can even make the function a little 'sexier' like this:
function myTrans(f) {
  f.mySel.options[f.mySel.length] = new Option(f.txt.value,