why
why

Reputation: 24851

How to select option after page loads with prototype?

<select class="goog-te-combo">
  <option value="">select</option>
  <option value="ja">japan</option>
</select>

After page has loaded, I want to select the option whose value is "ja", i want to use prototype to do this automatic, anyone can help me ? thanks!

Upvotes: 0

Views: 2543

Answers (2)

clockworkgeek
clockworkgeek

Reputation: 37700

The answer seems obvious:

$$('option[value=ja]').first().selected = true;

Upvotes: 4

Linus Kleen
Linus Kleen

Reputation: 34642

Assuming you assigned an id to the <select>:

(function(element) {
    $A(element.options).each(function(option, index) {
       if ('ja' == option.value)
           element.selectedIndex = index;
    });
})( $('select-id') );

To retrieve all <select> elements of a given class, do:

$$('select.class_name_here').each(function(element) {
    $A(element.options).each(function(option, index) {
       if ('ja' == option.value)
           element.selectedIndex = index;
    });
});

Please refrain from using "denglish" in your code; it makes it look unsexy.

Here's a fiddle for that

Upvotes: 1

Related Questions