For some reason I can't get this to work.
The list of my options 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 loads, the dropdown does not display Eric Hunt as preselected. Nothing like this.
how can i get "selected true" instead of "defaultSelected true".
EDIT: As it turned out, the code above works fine, thanks to the help of deceze and rosscj2533 below. The only reason it doesnβt work for me was to find a ruby ββcode that was rewriting selected elements.
Thanks for helping everyone,
Greetings
javascript jquery html
Eric hunt
source share