Skip to content
Related Articles
Get the best out of our app
Open App

Related Articles

jQuery querySelector() Vs querySelectorAll() Methods

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

querySelector() and querySelectorAll() are two jQuery functions which helps the HTML elements to be passed as a parameter by using CSS selectors (‘id’, ‘class’) can be selected.

querySelector() Method: The querySelector() method returns the first element within the document which matches a specified CSS selector(s). If multiple elements occurs, then it returns the result for only the first matching element.



It returns the first element which matches the selector.

querySelectorAll() Method: The querySelectorAll() method returns all the elements within the document which matches the specified CSS selector(s). It returns all the elements that matches with the selector in the form of a static NodeList object which is a collection of nodes. To access each element, we usually use a loop. Each element can be accessed via an index. The index starts with 0. The property length can be used to get the number of elements that matches the specified selector.



It returns all the elements which match the selector.


It returns the element at index i in the list.

Difference between querySelector() and querySelectorAll() Method:

HTML code:


<!DOCTYPE html>
<body style="text-align:center;">
    <h1 style="color:#006600">
    <div class="test-btn">text</div>
    <div class="test-btn">text</div>
    <div class="test-btn">text</div>
    <div class="test-btn">text</div>
    <button onClick="qselector()">
    <button onClick="qselectorall()">
        function qselector() {
                .style.color = "#006600";
        function qselectorall() {
            var x = document
            for (var i = 0; i < x.length; i++) {
                x[i].style.color = "#006600";


  • Before clicking any button:
  • After clicking querySelector button:
  • After clicking querySelectorAll button:

Differences: As seen above, querySelector() methodcan only be used to access a single element while querySelectorAll() method can be used to access all elements which match with a specified CSS selector. To return all matches, querySelectorAll has to be used, while to return a single match, querySelector is used.

My Personal Notes arrow_drop_up
Last Updated : 23 Dec, 2020
Like Article
Save Article
Similar Reads
Related Tutorials