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

06.07.2005
| 13045 views |
  • 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_elm.appendChild(option);
  };
</script>

<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>
</select>