Jason Swett
Jason Swett

Reputation: 45094

dojo.byId() works but dijit.byId() doesn't

I have a select field with id appointment_stylist_id. For some reason, the first one of these returns my element but the second one returns undefined:

  console.log(dojo.byId('appointment_stylist_id'));
  console.log(dijit.byId('appointment_stylist_id'));

Any idea why?

Upvotes: 6

Views: 15866

Answers (2)

Rajan
Rajan

Reputation: 111

dojo.byId("appointment_stylist_id");

Returns the element.

dijit.byId("appointment_stylist_id");

Returns the widget.

Using dijit.byId also you can get the value of the element like:

dijit.byId("appointment_stylist_id").getValue();

Upvotes: 1

Matt Ball
Matt Ball

Reputation: 359816

This is because dojo.byId does what you want (find a DOM element with a particular ID), and dijit.byId doesn't do that.

dijit.byId is a function for looking up a specific widget by its assigned name (id). This function is similar to dojo.byId but whereas dojo.byId returns DOMNodes, dijit.byId returns a JavaScript object that is the instance of the widget.

...

dijit.byId and dojo.byId are often confused, particularly by first time users. This function should be used when you wish to obtain a direct handle the the JavaScript object instance of your widget and access functions of that widget.

http://dojotoolkit.org/reference-guide/dijit/byId.html

See also

What the difference between dojo.byId and dijit.byId?

Upvotes: 15

Related Questions