6

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?

Jason Swett
  • 43,526
  • 67
  • 220
  • 351

2 Answers2

15

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?

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • Okay. Maybe this should be a separate question, but is there any easy way, then, to get the selected value of a select input? In jQuery, you just do `$("#appointment_stylist_id").val()`. – Jason Swett Apr 20 '11 at 00:10
  • [Most of the time](http://stackoverflow.com/questions/4651923#4652402), `dojo.byId('appointment_stylist_id').value` will work. Are you also using Dijit? See [this question](http://stackoverflow.com/questions/1850050/how-to-get-the-value-of-a-filteringselect-select-in-dojo) – Matt Ball Apr 20 '11 at 00:15
  • `.value` worked. Thanks. I'm not using Dijit. (I'd seen that question already but it didn't seem to apply.) – Jason Swett Apr 20 '11 at 00:21
  • If you prefer the chaining thing, you can do `dojo.query("#appointment_stylist_id").attr("value")` and even `var $ = dojo.query` :-) – peller Apr 20 '11 at 05:37
  • Wasn't use of attr() deprecated in 1.5 - In favour of the get/set helper methods. – Layke Apr 22 '11 at 12:00
1
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();
g00glen00b
  • 41,995
  • 13
  • 95
  • 133
Rajan
  • 111
  • 6