querySelectorAll() in javascript

querySelectorAll() method in javascript

querySelectorAll() method is used to retrieve all the descendant element of the given element.

Example

Try Yourself

Example Explanation

  • In the above example there is a table element, with th, tr and td elements.
  • Here first we retrieves the table element (document.getElementById("tableID");) and stored in elem1.
  • Next we retrieves all the descendant element, using the th value (elem1.querySelectorAll("th")) and stored it in elem2.
  • Using for loop all the values in the elem2 are displayed(alert(elem2[i].innerHTML);).