Add the "selected" attribute to a drop down option with jQuery
- by gaoshan88
How do I add the attribute "selected" to a drop down menu option based on that option matching some variable?
Here is how the dropdown is populated...
loginOptions = ["First", "Second", "Third];
var Login = $( '#Login' );
for ( var i = 0; i < loginOptions.length; i++ ) {
Login.append('<option value="'+ loginOptions[i] +'">'+ loginOptions[i] +'</option>');
}
I want to mark one option as "selected" based on whether or not its value matches another variable. so if loginOption[i] is equal to var existingLoginValue then set that option to 'selected'
Something that would do
if(loginOptions[i] === existingLoginValue){ print 'selected'; };
Thanks!