Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I select an option using prototype

People also ask

How do you make an option value selected?

You can select on any attribute and its value by using the attribute selector [attributename=optionalvalue] , so in your case you can select the option and set the selected attribute. $("div. id_100 > select > option[value=" + value + "]").


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;

To get the currently selected option, use:

$$('#mySelect option').find(function(ele){return !!ele.selected})

Try this:

$('mySelect').setValue(1); // or whatever value you want to select

this one would select option1


nils petersohn almost got it right, but typically, the option's "id" attribute is not what people are selecting against. this small change makes it work.

var selectThis = 'option1';
$$('select#mySelectId option').each(function(o) {
  if(o.readAttribute('value') == selectThis) { // note, this compares strings
    o.selected = true;
    throw $break; // remove this if it's a multi-select
  }
});

For selecting the second option by value you could use this:

var myChoice = '2';

$$('select#mySelectId option').each(function(o) {
    o.selected = o.readAttribute('value') == myChoice;
});

var itis = $(mySelectId).select('option[value="' + sValueToSelect + '"]');
if ( itis && itis.length > 0 )
    itis[0].selected = true;