DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Custom Value In Select Box

  • submit to reddit
<script type="text/javascript">
  function custom_select_val(select_elm, prompt_text){
    var val = prompt(prompt_text, '');
    var option = document.createElement('option');
    option.setAttribute('value', val);
    option.innerHTML = val;
    option.selected = true;

<select name="thing" onchange="if(this.value=='!') custom_select_val(this, 'Enter your custom value.')">
  <option value="static1">static item 1</option>
  <option value="static2">static item 2</option>
  <option value="static3">static item 3</option>
  <option value="!">[specify...]</option>