Inside-Out - Web Design & Development 4 Beginners

Text Field to Dropdown

December 30, 2009

Here’s a very simple method for adding (populating) content to a drop down option box in your form via a text-field using JavaScript. It was something I required a while back and found very little examples on the Internet.

Javascript

function Add_to_drop_down()
{
Name_text = document.getElementById('Box_text').value;
Val_text = Name_text
obj = document.getElementById("my_drop_down");
obj.options[obj.options.length] = new Option(Name_text,Val_text)
obj.selectedIndex=obj.options.length-1;
}

HTML

<form>
<input type=text id="Box_text">
<input type=button onclick="Add_to_drop_down()" value="Add &raquo;">
<select name="my_drop_down" id="my_drop_down"></select>
</form>

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Technorati
  • BlinkList
  • De.lirio.us
  • Furl
  • Reddit
  • StumbleUpon

Comments are closed.

Previous Post
« Change Page on Drop Down
Next Post
Simple PHP Gallery »