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);
This method takes one string argument.
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:
Code explanation
In the above code snippet:
Line 6-9: We create a
divelement with theparentid. We add anotherdivelement inside theparentelement with the idchild-1.Line 11: We load the Dojo library from the CDN server.
Line 13: We load the
dojo/dommodule that contains thebyIdmethod.Line 14: We use the
byIdmethod to get the DOM node that has theparentid. In our case, there is adivelement with the idparentpresent, so that element is returned.Line 17: We use the
byIdmethod to get the DOM node that has the idchild-2. In our case, there is nodivelement with the idchild-2, sonullis returned.
Free Resources