W3C home > Mailing lists > Public > public-script-coord@w3.org > January to March 2012

Re: querySelectorAll() -- selecting _immediate_ children of element

From: Tab Atkins Jr. <jackalmage@gmail.com>
Date: Mon, 9 Jan 2012 09:07:13 -0800
Message-ID: <CAAWBYDA=8aV41eSW9VtK4QH7m0wjsgSedckk301Wm2Rt5ph13w@mail.gmail.com>
To: "Marat Tanalin | tanalin.com" <mtanalin@yandex.ru>
Cc: public-script-coord@w3.org
On Mon, Jan 9, 2012 at 6:17 AM, Marat Tanalin | tanalin.com
<mtanalin@yandex.ru> wrote:
> querySelector() and querySelectorAll() methods are exciting features, but they do not allow select _immediate_ children of reference element.
>
> To address this lack, we could use following syntax:
>
>    var divs = refElement.querySelectorAll('> DIV');
>
> Here 'divs' variable contains list of DIV elements that are immediate children (not just descendant elements) of reference element (refElement).
[snip]

This is already supported in the Selectors API 2 draft:
<http://dev.w3.org/2006/webapi/selectors-api2/>

Using the qS and qSA functions, you can pass a reference node as the
second argument, and select it with the :scope pseudoclass, like so:

document.querySelectorAll(':scope > div', refElement)

Using the find and findAll functions, you can just start a selector
with a combinator, like in your example:

refElement.findAll('> div')

~TJ
Received on Monday, 9 January 2012 17:08:10 UTC

This archive was generated by hypermail 2.3.1 : Wednesday, 8 May 2013 19:30:05 UTC