Tuesday, October 3, 2023
 Popular · Latest · Hot · Upcoming
144
rated 0 times [  148] [ 4]  / answers: 1 / hits: 55295  / 14 Years ago, fri, august 7, 2009, 12:00:00

Say I have a HTML form containing this select element:



  <select name=mySelect id=mySelect>
<option value=1 id=option1>1</option>
<option value=2 id=option2>2</option>
</select>


How can I use prototype to select one of the option elements?



The methods listed in the API reference of Form.Element don't seem to help with this.



edit: by select I mean the equivalent effect of the selected attribute on an option element.


More From » html

 Answers
2
var options = $$('select#mySelect option');
var len = options.length;
for (var i = 0; i < len; i++) {
console.log('Option text = ' + options[i].text);
console.log('Option value = ' + options[i].value);
}


options is an array of all option elements in #mySelect dropdown. If you want to mark one or more of them as selected just use selected property



// replace 1 with index of an item you want to select
options[1].selected = true;

[#98971] Tuesday, August 4, 2009, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
yamileth

Total Points: 53
Total Questions: 96
Total Answers: 112

Location: England
Member since Tue, Sep 8, 2020
3 Years ago
;