1
votes

From my testing, it appears that dojo's query() function only selects from among elements that are part of a page's DOM.

For example, suppose I have a node named rootNode that has some number of descendents with the class someClass. This code:

var nodeList = query(".someclass", rootNode);

will return an empty NodeList if rootNode has been removed from the DOM (but, of course, if rootNode is part of the DOM, it will return all the nodes with class someClass.

My question: is there a way to use query() in this situation? If not, what is the preferred way for handling this? It looks to me like some of NodeList's methods can be used with a filter(remove and place), but that's not quite the same.

2
I don't understand what you are trying to do. - Jess
Sorry to be unclear. I have a div that I have removed from a page's DOM but have kept a reference to. Later on, I want to query that div to find some of its descendents that match particular criteria and then do something with those elements. - susie derkins

2 Answers

1
votes

You can get a dojo NodeList as the return result of your dojo.query. Then you can use nodes.map or nodes.every to find the elements you want.

http://dojotoolkit.org/reference-guide/1.9/dojo/NodeList.html

1
votes

My answer turned out to be very simple: although I didn't find it in the documentation, you can simply call query() on a NodeList. e.g.

var nodeList = query.NodeList();
nodeList.push(rootNode);
var results = nodeList.query(yourSelector);

Note, however: if yourSelector is a unique id and rootNode has been removed from the DOM, it doesn't appear to work. Other types of selectors (class, children, etc) seem to work. I wonder if that's a bug in Dojo...