DEV Community

Adam Crockett πŸŒ€
Adam Crockett πŸŒ€

Posted on

7 2

Query the DOM using ES6

πŸ€” Why do you need to include the language version? such simple task, you must be thinking.

Yes you,

I'm talking to you, the one with the haircut that's this long:
who me?

...wait we all have hair like this don't we?

Well believe it or not I'm about to teach you something so blindingly obvious, you'll kick yourself that you didn't spot it, call your relatives and tell them before forgetting about this again.

Let's query

Why do this?

const username = document.getElementById("#username");
const password = document.getElementById("#password");
Enter fullscreen mode Exit fullscreen mode

when you can do this ES6 de-structure that NodeList

const [username, password] = document.querySelectorAll("#username, #password");
Enter fullscreen mode Exit fullscreen mode

One call to query the DOM, but this only works for finite queries which can be determined, you know you have 2 IDs and so should not have any other elements returned, or perhaps you query on some other attribute, as long as the results are predictable this will work nicely.

When are results predictable, forms should be at least?

I submit that this isn't for everything πŸ’œ

Image of Bright Data

Maximize Data Efficiency – Store and process vast amounts efficiently.

Optimize your infrastructure with our solutions designed for high-volume data processing and storage.

Optimize Now

Top comments (0)

Imagine monitoring actually built for developers

Billboard image

Join Vercel, CrowdStrike, and thousands of other teams that trust Checkly to streamline monitor creation and configuration with Monitoring as Code.

Start Monitoring

πŸ‘‹ Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay