← All tutorials

GoCareerGo Tutorials

JavaScript Tutorial

Variables to closures, promises, the DOM and OOP — a complete JS reference.

Selecting Elements

Before you can change anything on a page, you need to select the DOM element(s) you want to work with.

document.getElementById('title');
document.querySelector('.card');       // first match
document.querySelectorAll('.card');    // NodeList of all matches
document.getElementsByClassName('card'); // live HTMLCollection
document.getElementsByTagName('li');
  • querySelector/querySelectorAll accept any CSS selector — most flexible
  • getElementById is fastest for a single known ID
  • querySelectorAll returns a static NodeList; getElementsBy* return a LIVE collection