Add the "selected" attribute to a drop down option with jQuery
Posted
by gaoshan88
on Stack Overflow
See other posts from Stack Overflow
or by gaoshan88
Published on 2010-03-11T19:58:21Z
Indexed on
2010/03/11
19:59 UTC
Read the original article
Hit count: 246
JavaScript
|jQuery
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!
© Stack Overflow or respective owner