Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between querySelectorAll and getElementsByTagName?

Tags:

javascript

I was wondering about two different syntax of selecting element in JavaScript.

suppose if I want to select all divs from current document then:

var divs = document.getElementsByTagName("div"); console.log("There are "+divs.length+" Divs in Document !"); 

Will work fine. But there is also another way of doing so, like:

var divs = document.querySelectorAll("div"); console.log("There are "+divs.length+" Divs in Document !"); 

When both of them works in the same way. What's the difference between them ?

  • Which one is faster?
  • Why?
  • How both works?
  • Thanks in advance. I've seen the questions like this but they didn't satisfied the need.

    like image 349
    Vedant Terkar Avatar asked Aug 15 '13 06:08

    Vedant Terkar


    People also ask

    What is the difference between querySelectorAll and Getelementsby *?

    The difference is in versatility and browser support. @Salman_A querySelectorAll is new and hence supported by new browsers. But getElementsByTagName is old and hence has great support.

    What is a difference between the querySelector and querySelectorAll methods?

    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.

    What does querySelectorAll mean?

    The querySelectorAll() method in HTML is used to return a collection of an element's child elements that match a specified CSS selector(s), as a static NodeList object. The NodeList object represents a collection of nodes. The nodes can be accessed by index numbers.

    Is getElementsByClassName faster than querySelectorAll?

    Specifically, getElementById() and getElementsByClassName() are more than twice as fast as querySelector() and querySelectorAll() .


    2 Answers

    Most answeres are wrong. Nicolae Olariu is the only, who answered correcly

    Which one is faster? Why?

    are not the questions. The real question "How it works?"

    The main difference is in this example:

    <!doctype html>  <html>  <head>      <meta charset="utf-8">      <title>Yandex</title>   </head>  <body>      <a href="((http://yandex.ru))">Яндекс</a>,      <a href="((http://yandex.com))">Yandex</a>  </body>  <script>  var elems1 = document.getElementsByTagName('a'), // return 2 lements, elems1.length = 2      elems2 = document.querySelectorAll("a");  // return 2 elements, elems2.length = 2   document.body.appendChild(document.createElement("a"));   console.log(elems1.length, elems2.length);  // now elems1.length = 3!                                              // while elems2.length = 2 </script> </html>  

    Because querySelectorAll returns a static (not live) list of elements.

    like image 126
    Artem G Avatar answered Oct 14 '22 00:10

    Artem G


    Selections

    getElementsByTagName only selects elements based on their tag name. querySelectorAll can use any selector which gives it much more flexibility and power.

    Return value

    • gEBTN returns a live node list.
    • qSA returns a static node list.

    Live node lists can be useful (you can query once, store the value, and have it update as the DOM changes) but are responsible for a lot of confusion such as the example in this question.

    Usually a static list is easier to deal with.

    Support

    See caniuse for gEBTN and qSA.

    gEBTN has more support, but qSA has support in all browsers that are relevant for most use cases today.

    Performance

    You probably shouldn't care. These functions are unlikely to be a bottleneck in your code.

    I've seen conflicting reports about which is faster. It likely varies between browsers anyway.

    like image 35
    Quentin Avatar answered Oct 13 '22 22:10

    Quentin