For some reason, I can’t get this to work.
My options list is populated dynamically using these scripts:
function addOption(selectId, value, text, selected) {
var html = '<option value="'+value+'">'+text+'</option>';
if (selected == "on") {
html = '<option value="'+value+'" selected="selected">'+text+'</option>';
}
$('#'+selectId).append(html);
}
function addSalespersonOption(id, name, defsales) {
addOption('salesperson', id, name, defsales);
}
Here is the HTML:
<td class="text-r"><label for="salesperson">Salesperson:</label></td>
<td>
<select id="salesperson">
<option value="">(select)</option>
</select>
</td>
So far, the output is:
<option value="1266852143634" selected="selected">Eric Hunt</option>
The DOM shows this:
index 2
disabled false
value "1266852143634"
text "Eric Hunt"
selected false
defaultSelected true
But for some reason, when the page is loaded, the dropdown does not display Eric Hunt as pre selected. Nor is anything for that matter.
How can I get “selected true” instead of “defaultSelected true”?
EDIT: As it turns out, the above code works perfectly, thanks to the help of deceze and rosscj2533‘s answers from below. The only reason it’s not working for me is, I found Ruby code that was overwriting the select elements.
Thanks for everyone’s help on this,
Cheers
The
defaultSelectedattribute is not settable, it’s just for informational purposes:Quote:
I think you want:
I.e. set the
selectedattribute of theoptionyou want to select.Without trying to fix your code, here’s roughly how I would do it:
You seem to have a lot of baggage and dependencies already and I can’t tell you how to best integrate the selected option into your code without seeing more of it, but hopefully this helps.