Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #39701 > unrolled thread

Single nodelist from multiple

Started byAndrew Poulos <ap_prog@hotmail.com>
First post2021-03-19 12:24 +1100
Last post2021-03-19 14:59 -0700
Articles 3 — 3 participants

Back to article view | Back to comp.lang.javascript


Contents

  Single nodelist from multiple Andrew Poulos <ap_prog@hotmail.com> - 2021-03-19 12:24 +1100
    Re: Single nodelist from multiple "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-03-18 18:50 -0700
      Re: Single nodelist from multiple "sc...@sauyet.com" <scott@sauyet.com> - 2021-03-19 14:59 -0700

#39701 — Single nodelist from multiple

FromAndrew Poulos <ap_prog@hotmail.com>
Date2021-03-19 12:24 +1100
SubjectSingle nodelist from multiple
Message-ID<wMqdnURasNrKYM79nZ2dnUU7-bnNnZ2d@westnet.com.au>
This code creates two nodelists:

const container = document.querySelector("#test");
const nodes = container.querySelectorAll("div");

const container2 = document.querySelector("#test2");
const nodes2 = container2.querySelectorAll("div");

I want to join both nodelists together to form one nodelist. And I need 
to keep them as a HTML collection rather than covert them to an array.

Is the only approach upadting the selector to cover all the nodes I want?

Andrew Poulos

[toc] | [next] | [standalone]


#39703

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2021-03-18 18:50 -0700
Message-ID<c355a50d-828b-49da-b9d7-124ffe4e1be2n@googlegroups.com>
In reply to#39701
On Thursday, March 18, 2021 at 8:24:50 PM UTC-5, Andrew Poulos wrote:
> This code creates two nodelists: 
> 
> const container = document.querySelector("#test"); 
> const nodes = container.querySelectorAll("div"); 
> 
> const container2 = document.querySelector("#test2"); 
> const nodes2 = container2.querySelectorAll("div"); 
> 
> I want to join both nodelists together to form one nodelist. And I need 
> to keep them as a HTML collection rather than covert them to an array. 
> 
> Is the only approach upadting the selector to cover all the nodes I want? 


I think that's the easiest:

```
const qsa = (sel, ctx) => (ctx || document).querySelectorAll(sel)

const nodeUnion = qsa('#test div, #test2 div')
```

[toc] | [prev] | [next] | [standalone]


#39713

From"sc...@sauyet.com" <scott@sauyet.com>
Date2021-03-19 14:59 -0700
Message-ID<2a20589d-3fe5-408f-ab8c-02852390713an@googlegroups.com>
In reply to#39703
Michael Haufe (TNO) wrote:
> Andrew Poulos wrote: 

>> I want to join both nodelists together to form one nodelist. And I need 
>> to keep them as a HTML collection rather than covert them to an array. 
>> 
>> Is the only approach upadting the selector to cover all the nodes I want?

> I think that's the easiest: 
> 
> ``` 
> const qsa = (sel, ctx) => (ctx || document).querySelectorAll(sel) 
> 
> const nodeUnion = qsa('#test div, #test2 div') 
> ```

Or perhaps

```
    const nodeUnion = (ctx) => (...sels) => ctx.querySelectorAll(sels.join(', '))
    const docNodes = nodeUnion (document)
    const nodes = docNodes ('#test div', '#test2 div')
```

  -- Scott

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web