Flowdock
select(pattern) public

Returns a collection reference by finding it through a CSS pattern in the DOM. This collection can then be used for further method calls. Examples:

  page.select('p')                      # => $$('p');
  page.select('p.welcome b').first      # => $$('p.welcome b').first();
  page.select('p.welcome b').first.hide # => $$('p.welcome b').first().hide();

You can also use prototype enumerations with the collection. Observe:

  page.select('#items li').each do |value|
    value.hide
  end
  # => $$('#items li').each(function(value) { value.hide(); });

Though you can call the block param anything you want, they are always rendered in the javascript as ‘value, index.’ Other enumerations, like collect() return the last statement:

  page.select('#items li').collect('hidden') do |item|
    item.hide
  end
  # => var hidden = $$('#items li').collect(function(value, index) { return value.hide(); });
Show source
Register or log in to add new notes.
March 27, 2009 - (>= v2.1.0)
1 thank

Gotcha with method calls inside select loop

Keep in mind that any methods you call on the object in the select loop will be strung together when the Javascript is rendered. For example:

page.select(".shipping_type_fee").each do |td|
  td.down("span").update("--").show
  td.down("img").hide
end

will be rendered as:

$$('.shipping_type_fee').each(function(value, index) {
  value.down("span").update("--").show().down("img").hide();
});

This is probably not what you want!