12

How can I (if it is possible) use the Prototype library to loop through all select elements on a page and access the element? In the documentation I found easily shortcuts for referencing elements with certain ids, class names etc. but no reference for elements with certain tag names.

If this is not possible with Prototype, an example with JQuery or another JS Library would be appreciated.

2 Answers 2

22

Check out the first example in this page:

$$('select').each(function() {
    //
});

Essentially, the $$ function expects a CSS selector, and a tag name is a perfectly valid selector.

Sign up to request clarification or add additional context in comments.

1 Comment

You can reference the items which you iterate adding a parameter to the function: each(function(item) { item.doSomething()...
4

A jQuery example, for variety:

$('select').each(function() {
    var selectedOption = $(this).find('option:selected');
    alert('Value: ' + selectedOption.val() + ' Text: ' + selectedOption.text());
});

That will iterate over all selects in the page, and alert the text and value of the selected option on each select.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.