Using CSS selectors with JavaScript DOM methods

coding
javascript
web
technology
launchschool
How I learned to use CSS selectors with JavaScript DOM methods like querySelector and querySelectorAll.
Author

Alex Strick van Linschoten

Published

November 12, 2020

I’ve been using JavaScript methods that interact with the DOM all week as part of my Launch School course. Among them, document.querySelector() and document.querySelectorAll() seem really useful. I realised I didn’t fully understand the selectors that you were supposed to pass in as an argument, so I’m writing up some of what I discovered about them here. (See here for the documentation).

The simple part is when you have a single selector. Three important selectors are:

When combining tags, there is a complex set of options depending on whether things are siblings or descendants or children. For the most part that is TMI. The important ones to remember are:

(For a more complete exploration of this topic, specifically the combination of selectors, read this blogpost.)