querySelectorAll()

a JavaScript function for selecting elements
  1. Welcome
  2. Examples
    - addEventListener()
    - Buttons
    - Data Attribute
    - document.
    querySelectorAll()

    - forEach()
    - Multiple Classes
    - to array
  3. Errors
    - Failed to execute 'querySelectorAll' on 'document'
  4. Advanced
  5. querySelectorAll() vs querySelector()
  6. Performance
  7. Troubleshooting

Links

  1. allowfullscreen
  2. Questions LLC

Advanced

The querySelectorAll() function is a powerful and versatile method for selecting elements in an HTML document using a CSS selector. While this function is commonly used for basic element selection, it can also be used in more advanced scenarios to achieve complex effects in web development. This technical documentation will describe a collection of advanced techniques for using querySelectorAll() in complex web development scenarios.

Selecting Nested Elements

One of the most common advanced uses for querySelectorAll() is to select nested elements within a parent element. This is particularly useful when you want to select all of the elements within a particular container, such as all of the <li> elements within a <ul> element. To do this, you can use the > (child combinator) in your CSS selector to specify that you only want to select direct children of the parent element. For example:

// Select all <li> elements that are direct children of a <ul> element const listItems = document.querySelectorAll("ul > li");

Selecting Sibling Elements

Another common use for querySelectorAll() is to select sibling elements that are adjacent to a given element. This is useful when you want to select elements that are related to a particular element, but not necessarily nested within it. To do this, you can use the + (adjacent sibling combinator) in your CSS selector to specify that you only want to select elements that are immediately adjacent to the given element. For example:

// Select the <h2> element that is immediately after the <h1> element const h2 = document.querySelectorAll("h1 + h2");

Selecting Elements Based on Attribute Values

Another advanced technique for using querySelectorAll() is to select elements based on the values of their attributes. This is useful when you want to select elements that have a specific value for a particular attribute, such as all <a> elements with a href attribute that starts with a certain string. To do this, you can use the [attr] and [attr=value] syntax in your CSS selector to specify the attribute and value that you want to match. For example:

// Select all <a> elements with a href attribute that starts with "https://" const httpsLinks = document.querySelectorAll('a[href^="https://"]');

Selecting Elements Based on Their Position in the Document

Another advanced use for querySelectorAll() is to select elements based on their position in the document. This is useful when you want to select elements that are the first, last, or nth child of their parent element, or when you want to select elements that are a certain number of siblings away from a given element. To do this, you can use the :first-child, :last-child, :nth-child(), and :nth-last-child() pseudo-classes in your CSS selector to specify the position of the elements you want to select. For example:

// Select the first <li> element in each <ul> element const firstListItems = document.querySelectorAll("ul li:first-child"); // Select the last <li> element in each <ul> element const lastListItems = document.querySelectorAll("ul li:last-child"); // Select the 3rd <li> element in each <ul> element const nthListItems = document.querySelectorAll("ul li:nth-child(3)");