Hunain Usman
Hunain Usman

Reputation: 2228

How to change dropdown combobox value using Javascript

I know this may be kind of a simple question, but cant seem to find an answer anywhere, I have a form in my site, with multiple form elements like combobox, text, radio buttons, checks etc, My requirement is to change the values of those form elements when a user from dropdown combobox is selected, I have successfully implemented that functionality with all the form elements except for the comboboxes, I cant seem to find any way to change the options of the combobox...

Upvotes: 0

Views: 688

Answers (2)

Midhun Murali
Midhun Murali

Reputation: 2151

New option can be added to the combobox as below

$('< option value=’Value’ > Sample Value < /option >').appendTo('#ComboId');

a dropdown value can be selected by

$('#ComboId').val('Value');

Upvotes: 0

Paul Draper
Paul Draper

Reputation: 83205

Since you included the jQuery tag,

select.val(value);

where select is the jQuery select element and value is the value.

JSFiddle

<select>
    <option value="1">First option</option>
    <option value="2">Second option</option>
    <option value="3">Third option</option>
</select>

<script>
    $('select').val('2');
</script>

Upvotes: 2

Related Questions