question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule proposal: `no-query-selector-all-zero`

See original GitHub issue

Avoid selecting an entire NodeList when only the first item is desired.

Fail

document.querySelectorAll("form")[0].addEventListener("submit", submitFunction);

Pass

document.querySelector("form").addEventListener("submit", submitFunction);

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:4
  • Comments:11 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
sindresorhuscommented, Jul 7, 2021

Another fail:

document.querySelectorAll("form").at(0).addEventListener("submit", submitFunction);
2reactions
fregantecommented, Jan 26, 2022
document.querySelectorAll('[data-foo=2]')

The number needs to be quoted.

I’m not sure you’d want to venture into validating CSS selectors, but if you do, this would probably deserve its own rule or even its own plugin.

Read more comments on GitHub >

github_iconTop Results From Across the Web

document.querySelectorAll() function not working on all ...
querySelectorAll returns a collection of elements. The DOM doesn't have a "set-based" feature letting you assign handlers to the entire set ...
Read more >
Document.querySelectorAll() - Web APIs | MDN
The Document method querySelectorAll() returns a static (not live) NodeList ... its length property is 0), then no matches were found.
Read more >
Is there any analogue to querySelectorAll in SAPUI5?
What I need to do is to select a number of controls by class and add addEventDelegate method to each of them. Is...
Read more >
Selectors Level 3 - W3C
A Proposed Recommendation is a document that has been widely ... (When the selectors are not grouped, only the rule for h2..foo is...
Read more >
Styling local DOM - Polymer Project
Polymer uses Shadow DOM styling rules for providing scoped styling of the ... 1.6.0 and later accepts @apply without parenthesis, matching the proposal....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found