How can I change <select> box <option> values dynamically to new list?
Let say, I have selec box like below:
<select class="form-control selectOptions">
<option>Default Option 1</option>
<option selected>Default Option 2</option>
<option>Default Option 3</option>
</select>
and above option values has to replaced with:
<select class="form-control selectOptions">
<option>New Option 1</option>
<option>New Option 2</option>
<option>New Option 3</option>
<option selected>New Option 4</option>
<option>New Option 5</option>
</select>
I have tried below code:
$(document).ready(function(){
var selectBox = $("select.selectOptions");
selectBox.empty();
$("<option value='new_option_1'>New Option 1</option>").appendTo(selectBox);
$("<option value='new_option_2'>New Option 2</option>").appendTo(selectBox);
$("<option value='new_option_3'>New Option 3</option>").appendTo(selectBox);
$("<option value='new_option_4'>New Option 4</option>").appendTo(selectBox);
$("<option value='new_option_5'>New Option 5</option>").appendTo(selectBox);
$("select.selectOptions option:eq(3)").attr("selected", "selected");
});
but it will be hard to change if I have more options.
So, how can I create an array like below...
And <option> value also should be dynamic.. Eg: For New Option 4, value should be new_option_4
var myNewOptions = [
New Option 1,
New Option 2,
New Option 3,
New Option 4,
New Option 5
];...