I am bit confused with this function. Should I be able to pass in a DOM element e.g. someAjaxRequest.responseXML and interrogate it ? Seems not but perhaps I am missing something... TIA Matt
Matt Spendlove wrote:> I am bit confused with this function. Should I be able to pass in a DOM > element e.g. someAjaxRequest.responseXML and interrogate it ?You can think of $$() as $() on steroids. Instead of simply taking an id and returning the element it will take a CSS selector and return all matching elements. So: $$(''#news .item'') will return all elements with "item" in the class names that are children of the element with id "news". Eric
Hey Eric I understand what the function does, I am just not sure whether the search applies only to the current document or if you can pass in some DOM fragment e.g. the responseXML from an AJAX call.. I suspect not from trying it out and looking at the code but I am not certain... Any thoughts welcome Matt Eric Anderson wrote:> Matt Spendlove wrote: >> I am bit confused with this function. Should I be able to pass in a >> DOM element e.g. someAjaxRequest.responseXML and interrogate it ? > > You can think of $$() as $() on steroids. Instead of simply taking an > id and returning the element it will take a CSS selector and return > all matching elements. So: > > $$(''#news .item'') > > will return all elements with "item" in the class names that are > children of the element with id "news". > > Eric > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs >
Matt Spendlove wrote:> I understand what the function does, I am just not sure whether the > search applies only to the current document or if you can pass in some > DOM fragment e.g. the responseXML from an AJAX call..Gotcha, just misunderstood your question. The search applies to the current document. If you want it to only apply to a portion of the document you would need to put an identifier on that portion and prepend that identifier to your search criteria. You might be able to use a DOM fragment but you would have to use the Selector objects directly instead of using the $$() function. So something like: s = new Selector(''.item''); items = s.findElements(dom_fragment); I have never tested the above code but it seems it should work. The only catch is that using the selector object will only allow you to use the basic selector syntax. You cannot specify element relationships. For example the following would not work: s = new Selector(''#news .item''); news_items = s.findElements(dom_fragment); If you want to specify relationships and use a dom_fragment you would probably need to do something like this (borrowed from the $$() implementation): function $$WithFragment(expression, fragment) { return expression.strip().split(/\s+/).inject( [fragment], function(results, expr) { var selector = new Selector(expr); return results.map( selector.findElements.bind(selector)).flatten(); }); }) Of course none of the code is tested but that should give you the general idea. Eric
Hey Eric Thanks very much for the explanation.. I''ll play with the Selector class.. Cheers Matt Eric Anderson wrote:> Matt Spendlove wrote: >> I understand what the function does, I am just not sure whether the >> search applies only to the current document or if you can pass in >> some DOM fragment e.g. the responseXML from an AJAX call.. > > Gotcha, just misunderstood your question. > > The search applies to the current document. If you want it to only > apply to a portion of the document you would need to put an identifier > on that portion and prepend that identifier to your search criteria. > > You might be able to use a DOM fragment but you would have to use the > Selector objects directly instead of using the $$() function. So > something like: > > s = new Selector(''.item''); > items = s.findElements(dom_fragment); > > I have never tested the above code but it seems it should work. The > only catch is that using the selector object will only allow you to > use the basic selector syntax. You cannot specify element > relationships. For example the following would not work: > > s = new Selector(''#news .item''); > news_items = s.findElements(dom_fragment); > > If you want to specify relationships and use a dom_fragment you would > probably need to do something like this (borrowed from the $$() > implementation): > > function $$WithFragment(expression, fragment) { > return expression.strip().split(/\s+/).inject( > [fragment], function(results, expr) { > var selector = new Selector(expr); > return results.map( > selector.findElements.bind(selector)).flatten(); > }); > }) > > Of course none of the code is tested but that should give you the > general idea. > > Eric > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs >