Search code examples
prototypejselement

Prototype $$ returns array, should return one element like $


When using the dollar-dollar-function in prototype I alway get an array of elements back, instead of just one element with the dollar-function. How can I combine the power of CSS-selectors of $$ but still get only one element back?

Changing the structure of the source is not possible, so I can't just select it with the id. It needs to get selected with CSS, but should just return one element.


Solution

  • It does not make sense to return a single element when selecting by class name because potentially there could be many elements in the DOM that have this class. So you could always use the first element of the returned array if you are sure that it will be unique.

    $$('.foo')[0]