JS Basic Tutorial
JavaScript provides several methods to interact with and manipulate the Document Object Model (DOM). The DOM represents the structure of a web page as a hierarchical tree of elements, and these methods allow you to access specific parts of the document for dynamic updates.
Method |
Returns |
Live/Static |
Use Case |
---|---|---|---|
Single element |
Live |
Best for accessing unique elements by |
|
HTMLCollection |
Live |
Access multiple elements with the same class. |
|
HTMLCollection |
Live |
Access elements by their tag name. |
|
Single element |
Static |
Access the first element matching a CSS selector. |
|
NodeList |
Static |
Access all elements matching a CSS selector. |
document.getElementById()
id
attribute.null
if not found.Try it yourself
document.getElementByClassName()
Try it yourself
document.getElementByTagName()
Try it yourself
document.querySelector()
null
if not found.Try it yourself
document.querySelectorAll()
Try it yourself
When accessing multiple elements (e.g., using getElementsByClassName
or querySelectorAll
), you can loop through them to perform actions:
Try it yourself