Do all html nodes have a "getElementsBy" and getElementBy" version?

calvincoxiii picture calvincoxiii · Aug 7, 2017 · Viewed 10.6k times · Source

I'm learning vanilla js and something that keeps coming up is that I see some examples of code that say document.getElementBy... or document.getElement(s)By..., Is it the case that every html node has a corresponding js dom form where getElementBy refers to a single node and getElementsBy refers to a nodeList?

Answer

Rounin picture Rounin · Aug 8, 2017

Principal element gathering methods from the DOM API are:

  • document.getElementById('[ID]') // returns live HTML Element Object
  • document.getElementsByClassName('[CLASS]') // returns live HTML Collection Object
  • document.getElementsByName('[NAME]') // returns live HTML Collection Object
  • document.getElementsByTagName('[ELEMENT-TYPE]') // returns live HTML Collection Object

and

  • document.querySelector('[CSS-SELECTOR]') // returns static HTML Element Object
  • document.querySelectorAll('[CSS-SELECTOR]') // returns static NodeList Object