DEV Community

Cover image for JavaScript HTML DOM API
Vigneshwaran V
Vigneshwaran V

Posted on

JavaScript HTML DOM API

What is API?

An API (Application Programming Interface) is a set of rules that allows two software programs to communicate with each other.
Think of it like a waiter in a restaurant:

  • You (the user) place an order.

  • The waiter (API) takes your order to the kitchen.

  • The kitchen (server/application) prepares the food.

  • The waiter (API) brings the food back to you.
    You don't need to know how the kitchen works—you just use the waiter to get what you need.
    Real-world example
    Suppose you use a weather app:

  • You open the app and search for "New York."

  • The app sends an API request to a weather service.

  • The weather service processes the request and sends back the current temperature and forecast.

  • The app displays the information.

Simply, an API is a messenger that enables different software systems to communicate and exchange information.


What is HTML DOM API?

The HTML DOM API is a collection of methods, properties, and objects provided by the browser that allows JavaScript to interact with and manipulate an HTML document.

What is DOM?

DOM (Document Object Model) is a tree-like representation of an HTML page.
For Example

<body>
    <h1 id="title">Hello</h1>
    <button>Click Me</button>
</body>
Enter fullscreen mode Exit fullscreen mode

The browser converts it into a DOM tree:

Document
  |
  └── html
       |
       └── body
            |
            ├── h1
            |    └── "Hello"
            |
            └── button
                 └── "Click Me"
Enter fullscreen mode Exit fullscreen mode

What is the DOM API?

The browser provides JavaScript with APIs to access and modify this DOM tree.
Examples:
1. Finding an element

const heading = document.getElementById("title");
Enter fullscreen mode Exit fullscreen mode

Here:

  • document is a DOM object.

  • getElementById() is a DOM API method.
    2. Changing text

heading.innerText = "Welcome";
Enter fullscreen mode Exit fullscreen mode

Output

<h1 id="title">Welcome</h1>
Enter fullscreen mode Exit fullscreen mode

Why is it called an API?
Because the browser exposes a set of functions and properties that JavaScript can use.

We use:

document.getElementById(); // DOM API
Enter fullscreen mode Exit fullscreen mode

DOM = the browser's representation of the HTML document.
DOM API = the methods and properties the browser provides to access and modify that DOM using JavaScript.

  • That's why methods like getElementById(), querySelector(), createElement(), and properties like innerText, src, and style are all considered part of the HTML DOM API.

good way to think about it.

The communication is actually:

JavaScript
     │
     ▼
DOM API (provided by browser)
     │
     ▼
DOM Tree (HTML document)
     │
     ▼
Browser updates the webpage
Enter fullscreen mode Exit fullscreen mode

For example:

<h1 id="title">Hello</h1>
Enter fullscreen mode Exit fullscreen mode

When JavaScript runs:

document.getElementById("title").innerText = "Welcome";
Enter fullscreen mode Exit fullscreen mode

Here's what happens:

  • JavaScript calls the DOM API method getElementById().

  • The browser's DOM API searches the DOM tree for the element whose id is "title".

  • It returns a reference of that < h1 > element to the JavaScript.

  • JavaScript changes its innerText property.

  • The browser updates the page and displays:

<h1 id="title">Welcome</h1>
Enter fullscreen mode Exit fullscreen mode

So yes, JavaScript does not directly communicate with HTML source code. Instead:

  • HTML → converted into a DOM tree by the browser.

  • Browser exposes the DOM API.

  • JavaScript uses the DOM API.

  • DOM API manipulates the DOM tree.

  • Browser reflects those changes on the webpage.
    That's why methods like:

document.getElementById()
document.querySelector()
document.createElement()
element.remove()
element.setAttribute()
Enter fullscreen mode Exit fullscreen mode

are called DOM APIs—they are functions provided by the browser for JavaScript to work with HTML documents.

The browser provides the DOM API to JavaScript. JavaScript uses this API to access and modify the DOM representation of the HTML document, which changes the structure, content, or properties of HTML elements on the webpage.


There are several ways to access HTML elements using the DOM API.

1. getElementById()
Accesses an element by its id.

<h1 id="title">Hello</h1>
Enter fullscreen mode Exit fullscreen mode
const element = document.getElementById("title");
Enter fullscreen mode Exit fullscreen mode

Returns a single element.

2. getElementsByClassName()
Accesses elements by their class name.

<p class="text">One</p>
<p class="text">Two</p>
Enter fullscreen mode Exit fullscreen mode
const elements = document.getElementsByClassName("text");
Enter fullscreen mode Exit fullscreen mode

Returns an HTMLCollection (similar to an array).

console.log(elements[0]); // Access the first paragraph
console.log(elements[1]); // Access the second paragraph
Enter fullscreen mode Exit fullscreen mode


3. getElementsByTagName()
Accesses elements by tag name.

<p>Hello</p>
<p>World</p>
Enter fullscreen mode Exit fullscreen mode
const paragraphs = document.getElementsByTagName("p");
Enter fullscreen mode Exit fullscreen mode
console.log(paragraphs[0]); // First paragraph
console.log(paragraphs[1]); // Second paragraph
Enter fullscreen mode Exit fullscreen mode

Returns an HTMLCollection.

HTMLCollection
Enter fullscreen mode Exit fullscreen mode


4. querySelector()
Accesses the first element that matches a CSS selector.

<p class="text">One</p>
<p class="text">Two</p>
Enter fullscreen mode Exit fullscreen mode
const element = document.querySelector(".text");
Enter fullscreen mode Exit fullscreen mode

Returns only the first matching element.
You can use:

document.querySelector("#title"); // id
document.querySelector(".text");  // class
document.querySelector("p");      // tag
Enter fullscreen mode Exit fullscreen mode


5. querySelectorAll()
Accesses all elements matching a CSS selector.

<p class="text">First</p>
<p class="text">Second</p>
<p class="text">Third</p>
Enter fullscreen mode Exit fullscreen mode
const elements = document.querySelectorAll(".text");
Enter fullscreen mode Exit fullscreen mode

Returns a NodeList.

NodeList(3) [p.text, p.text, p.text]
Enter fullscreen mode Exit fullscreen mode

You can expand it:

NodeList(3)
  0: <p class="text">First</p>
  1: <p class="text">Second</p>
  2: <p class="text">Third</p>
Enter fullscreen mode Exit fullscreen mode

Accessing elements
Just like an array:

console.log(elements[0]); // First <p>
console.log(elements[1]); // Second <p>
console.log(elements[2]); // Third <p>
Enter fullscreen mode Exit fullscreen mode
elements.forEach(element => {
    console.log(element);
});
Enter fullscreen mode Exit fullscreen mode

Checking the type

const elements = document.querySelectorAll(".text");

console.log(elements);
console.log(elements.length);
Enter fullscreen mode Exit fullscreen mode

Output:

NodeList(3)
3
Enter fullscreen mode Exit fullscreen mode

6. document.getElementsByName()
getElementsByName() is a DOM API method that selects all HTML elements that have a specified name attribute and returns them as a collection.

<input type="text" name="username">
<input type="text" name="username">
Enter fullscreen mode Exit fullscreen mode
const inputs = document.getElementsByName("username");

console.log(inputs);
Enter fullscreen mode Exit fullscreen mode

Output

NodeList(2)
Enter fullscreen mode Exit fullscreen mode
console.log(inputs[0]); // First input
console.log(inputs[1]); // Second input
Enter fullscreen mode Exit fullscreen mode


Which one is most used?

Nowadays developers mostly use:

document.querySelector();
document.querySelectorAll();
Enter fullscreen mode Exit fullscreen mode

because they work with CSS selectors, making them very flexible.
Examples:

document.querySelector("#title");
document.querySelector(".btn");
document.querySelector("div p");
document.querySelectorAll(".card");
Enter fullscreen mode Exit fullscreen mode

References

https://www.w3schools.com/js/js_htmldom_methods.asp
https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API

Top comments (0)