Possible Duplicate:
When using querySelectorAll, is there a way to reference the immediate children of the context node, without using IDs?
What I'm trying to do is to check if given element matches css selector. And as far as I know only way for it is to get parent node and then loop over what querySelectorAll find and finally check if in found nodes are the same node that I start with. But the thing is how to make the pool I must loop smaller.
code:
function querySelectorMatch(node,query) {
var found = node.parentNode.querySelectorAll(query);
for (var i = 0; i<found.length; i++) {
if (found[i] === node) return true;
}
return false;
}
Ok, this works, but the loop is consuming I think too many resources. Ok , I can check if parent node has id and or class and append that to the query that is parent#id.class > query
etc... but is it not going to be the same in terms of resources affected since code must do the same decisions anyway, but it will be closer to hardware... I don't know.
(Please no jQuery.)