What is byId() in dojo/dom?

Dojo is a JavaScript toolkit (library) that provides many utility modules and functions to create a web application. We can save time and speed up the development process using the predefined Dojo modules. Dojo includes language utilities, UI components, and more.

The byId() method in dojo/dom

The byId method can be used to get the DOM node that matches the given id. This method is equivalent to the document.getElementById method in JavaScript. The byId method is present in the dom module of the Dojo library.

Syntax

byId(id);
Syntax of byId() method

This method takes one string argument.

  1. id: This is the id of the DOM node to be returned.

Instead of id string, we can also pass the node element as an argument. In this case, the argument node passed as an argument is returned.

Return Value

This method returns the DOM node that matches the given id . If there is no element that matches the given id, null or undefined is returned based on the browser.

Code example

Let's look at the code below:

Console
Using the byId() method to query the DOM node by id attribute

Code explanation

In the above code snippet:

  • Line 6-9: We create a div element with the parent id. We add another div element inside the parent element with the id child-1 .

  • Line 11: We load the Dojo library from the CDN server.

  • Line 13: We load the dojo/dom module that contains the byId method.

  • Line 14: We use the byId method to get the DOM node that has the parent id. In our case, there is a div element with the id parent present, so that element is returned.

  • Line 17: We use the byId method to get the DOM node that has the id child-2. In our case, there is no div element with the id child-2, so null is returned.

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved