Reputation: 1144
I have this for loop which will append options to that "optlist"; the value will be set to manSel[i][0] and the text to manSel[i][1]; I would also like to set the attribute selected="selected" to all of them. Can I do that in the same loop or should I iterate again throug the options and add that attribute to each one of them. Thanks in advance!
for(var i = 0 ; i<manSel.length; i++){
optlist.append($('<option/>').attr('value', manSel[i][0]).text(manSel[i][1]));
}
Upvotes: 0
Views: 215
Reputation: 94101
To add more attributes use an object, this is very well explained in the jQuery Docs, I mean this is pretty basic stuff to find:
$ele.attr({ href: '', src: '' })
In any case value
has a special method val()
:
$ele.val(value)
And selected
is a property is meant to be used with prop()
.
$ele.prop('selected', true)
Upvotes: 1
Reputation: 32823
you could use this
optlist.append($('<option selected="selected"/>').attr('value', manSel[i][0]).text(manSel[i][1]));
Upvotes: 2
Reputation: 296
Try this:
for(var i = 0 ; i<manSel.length; i++){
optlist.append($('<option/>').attr({value: manSel[i][0], selected: "selected"}).text(manSel[i][1]));
}
Upvotes: 2