user663724
user663724

Reputation:

Confusion using dijit.byId and dojo.byId

I am trying to understand the difference between dijit.byId and dojo.byId

For this i have taken a Button and a div .(To set the data inside the div on click of the Button)

Show Me!

<div id="findMe">
    Hiya!
</div>

This is working (dojo.byId)

function callMe()
{
  var node = dojo.byId('findMe');
 node.innerHTML = "Hello World";
 }

But this isn't working (dijit.byId)

 function callMe()
    {
      var node = dijit.byId('findMe');
     node.innerHTML = "Hello World";
     }

My understanding is , when refering to the div we need to use dojo.byId and when we are refering to Individual components use dijit.byId

Please correct me if i am wrong .

Upvotes: 1

Views: 2668

Answers (2)

emboss
emboss

Reputation: 39630

As previously stated, dojo.byId retrieves the DOM node with that id if existent.

dijit.byId retrieves the instance of a dijit._Widget (and its subclasses), that is dojo's abstraction of UI objects, rather than the widget's DOM node. But it is important to note that dijit.byId searches through the widgets by the atrribute "widgetId", not "id". These are equal if you declare your widgets by passing a container node that already has an "id", but still dojo creates an attribute "widgetId" for every widget if not specified explicitly.

This means that widgetId and id are usually the same, but it is possible that they are different. Plus, widgetId is always set for a given widget even in cases where the id attribute of the container node is absent.

This implies that you should use dojo.byId whenever you intend to work on the DOM tree itself and dijit.byId only in case where you'd like to obtain the instance of a certain widget instance. If no widgets are present, there is no reason to use dijit.byId at all.

Upvotes: 5

Steffen
Steffen

Reputation: 2237

You are right:

  • dojo.byId finds elements in the DOM tree of your website with a certain ID - it searches and returns HTML elements.
  • dijit.byId finds dijits you created with a certain id - it searches and returns dijits (javascript objects), although these objects usually refer to a certain DOM node.

Upvotes: 4

Related Questions