I have a dropdown menu: Code. The options of "code" come from the database and the dropdown is populated. I have a textbox: Name. For certain values populated in Name (again from database), I want to replace the dropdown menu options with my own set of options, essentially overwriting the ones that are already there. Is there a way to do this?
My Code: HTML:
<div>
<table>
<tbody><tr><td><label>Code</label></td><td>
<select class="codeSelection"></select></td></tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody><tr><td><label>Name</label></td>
<td><input type="text" class="nameInput"></td></tr>
</tbody>
</table>
</div>
Code that I tried so far: (followed How do I add options to a DropDownList using jQuery?)
$('.nameInput').html("");
$('.nameInput').val(data[0].codeValue);
if ($('.nameInput').val() =="Smith")
{
var myOptions = {
val1 : "Tom",
val2 : "John"
};
$.each(myOptions, function(val, text){
$('.codeSelection').append($('<option></option>').val(val).html(text));
});
}
This still gives me the old dropdown and just displays the values "Tom" and "John" as text.