I have some jquery-like function:
function(elem) {
return $('> someselector', elem);
};
The question is how can i do the same with querySelector()
?
The problem is >
selector in querySelector()
requires parent to be explicitly specified. Is there any workaround?
Complete :scope polyfill
As avetisk has mentioned Selectors API 2 uses
:scope
pseudo-selector.To make this work in all browsers (that support
querySelector
) here is the polyfillUsage
For historical reasons, my previous solution
Based on all answers
Usage
There’s a query-relative lib, which is quite handy replacement for query-selector. It polyfills children selector
'> *'
and:scope
(inc. IE8), as well as normalizes:root
selector. Also it provides some special relative pseudos like:closest
,:parent
,:prev
,:next
, just in case.CLAIM
Personally I would take the answer from patrick dw, and +1 his answer, my answer is for seeking alternative solution. I don't think it deserves a downvote.
Here is my attempt :
see http://jsfiddle.net/Lgaw5/8/
check if element have id else add random id and do search based on it
will do same thing as