Javascript: querySelector vs getElementById


I’ve realized that most of people usually don’t use the querySelector, thus I decided to write quick examples and the basic differences between querySelector and selectElementById as well as querySelectorAll and getElementsByClassName.

Selectors, which are widely used in CSS, are patterns that match against elements in a tree structure. The Selectors API specification defines methods for retrieving Element nodes from the DOM by matching against a group of selectors. It is often desirable to perform DOM operations on a specific set of elements in a document. These methods simplify the process of acquiring specific elements, especially compared with the more verbose techniques defined and used in the past.

querySelector vs selectElementById

Let’s assume the following code:

<form id="userForm">
  <input id="username" type="text" value="Guilherme" />

We want to get the username element and allocate it in a variable. First let’s do this using getElementById.

var username = document.getElementById("username");

Now, let’s get the same element but this time using querySelector.

var username = document.querySelector("#userForm #username");

As you can see, using the querySelector the element that we want to select is much more specific and defined. The result is exactly the same.

getElementById image 1

querySelectorAll vs selectElementByClassName

Let’s assume the following code:

<form id="productForm">
  <input id="productOne" class="product" type="text" value="Product 1" />
  <input id="productTwo" class="product" type="text" value="Product 2" />
  <input id="productThree" class="product" type="text" value="Product 3" />

We want to get all the products elements and allocate them in a variable. First let’s do this using getElementByClassName.

var products = document.getElementsByClassName("product");

Now, let’s get the same elements but this time using querySelectorAll.

var products = document.querySelectorAll("#productForm .product");

The result is almost the same, but the getElementsByClassName returns a HTMLCollection and the querySelectorAll returns a NodeList.

getElementById image 2

Great! But these examples are very simple, so let’s try something a bit more difficult.

<table id="tableProducts">

In the example above, we have a table of products and we need to sum the values. I guess that there are some ways to do it, but let’s use querySelectorAll.

var sum = 0;
var cells = document.querySelectorAll("#tableProducts>tbody>tr>td:nth-of-type(2)");

for (var i = 0; i < cells.length; i++)
  sum += parseFloat(cells[i];

Very simple, isn’t it?


The querySelector and querySelectorAll are really very powerful and work well when you need to select more specific elements. They are supported by most of current browsers. If you wish, here you can check the supported browsers:

Share this postShare on FacebookDigg thisShare on Google+Share on LinkedInShare on RedditTweet about this on Twitter