Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User is able to add option to dropdown list by typing it in a text box

Is there any HTML/JavaScript code that can allow the viewer of a webpage to add a option to a dropdown select list by typing it in a text box???

like image 262
Adam Crookes Avatar asked Oct 19 '25 13:10

Adam Crookes


1 Answers

Very basic example. No CSS, fancy formatting, error checking, anything. The value of the new option will be identical to the displayed string.

function newOpt() {
  var $inp = $('#newOptIpt');
  var v = $inp.val();
  $('#modSelect').append('<option value="' + v + '">' + v + '</option>');
  $inp.val('')
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<select id="modSelect">
  <option value="default-1">Default option</option>
</select>
<br><br>
<input id="newOptIpt"/>
<button onclick="newOpt();">Add Option</button>
like image 172
Kwarrtz Avatar answered Oct 21 '25 02:10

Kwarrtz