Search code examples
javascriptarraysprototypejs

How to use PrototypeJS .wrap() on a $$('selector') array in a single statement?


Coming from a jQuery background onto a project using PrototypeJS.

$('someId');

In PrototypeJS will return the element with the given id.

$$('ul.someClass li');

will return the the elements matching the CSS selector, but in a document-ordered array.

How can I run wrap() on the elements in the console, on a single line, since the iterator argument on each() needs to be a function that only takes a collection element and an index.


Solution

  • Use the Enumerable#invoke method to apply your method and arguments to the collection. Just tried the following, and it worked on the first try:

    $$('p').invoke('wrap', 'div', {className: 'foo'});