querySelector search immediate children [duplicate]
Though it's not a full answer, you should keep an eye on the W3C Selector API v.2 which is already available in most browser, both desktop and mobile, except IE (Edge seems to support): see full support list.
function(elem) {
return elem.querySelectorAll(':scope > someselector');
};
You can't. There's no selector that will simulate your starting point.
The way jQuery does it (more because of a way that qsa
behaves that is not to their liking), is that they check to see if elem
has an ID, and if not, they temporarily add an ID, then create a full selector string.
Basically you'd do:
var sel = '> someselector';
var hadId = true;
if( !elem.id ) {
hadID = false;
elem.id = 'some_unique_value';
}
sel = '#' + elem.id + sel;
var result = document.querySelectorAll( sel );
if( !hadId ) {
elem.id = '';
}
This certainly isn't jQuery code, but from what I remember, it is basically what they do. Not just in this situation, but in any situation where you're running a selector from the context of a nested element.
Source code for Sizzle