0
votes

I have used select tag in my code and I want to assign value to that dropdown dynamically but I want to assign that value other form options that are assigned to that select tag

For example, if I have:

<select id="city" name="city">
<option value"Solapur">Solapur</options>
<option value"Pune">Pune</options>
<option value"Delhi">Delhi</options>
<option value"Mumbai">Mumbai</options>
</select>

And with the help of jQuery I want to assign value to combo box other than just above four.

For ex. I want to assign city as "Kolhapur" which is not in options.

Is it possible via jQuery?

1
Should the option get added to the combo box?Ja͢ck

1 Answers

2
votes

Just create a new option and insert it with one the many DOM insertion methods, depending on where you'd like to insert it :

var new_option = $('<option />', {value: 'Kolhapur', text: 'Kolhapur'});

$('#city').append(new_option);

or if you'd rather just change one of the existing options:

$('#city').find('option[value="Pune"]').val('Kolhapur').text('Kolhapur');