views:

969

answers:

2

Hi,

I've been through the Dojo docs as well as the API and tried Google but can't find a solution to my problem, I hope anybody here can help me out.

I'm trying to create a dijit.form.select programmatically (using Dojo 1.4) and connect to the "onClick"-event of the widget. Here's a part of my code:

var dataSelect = new dijit.form.Select({
        id    : "myselect",
        name  : "myselect",
        labelAttr: "label",
        labelType: "html"
      },
      "selectid");
      dataSelect.addOption({value: "value", label: "first item label"});

      dojo.connect(dataSelect, "onClick", function() {
        alert("clicked!");
      });

What it does: A select-box is created replacing an input-field with the ID "selectid", an option "first item label" is created. Everythings all right until here. Then I connect to the "onClick"-event of the select, which is supposed to load more options via AJAX (but will just display an alert for testing purposes in this example).

The problem: When I click on the little arrow next to the dropdown, the event is fired (OK). But when I click on the select box itself (the area containing the option), the event is NOT fired the first time I click it (unless I clicked on the arrow before). When I click the select box a second time (and every time after that), the event will fire!

I've tried to use "onFocus" instead of "onClick" which does work, but then the dropdown will not open when first clicked, even if I use the "openDropDown"-function (which does work when connecting to "onClick"!).

Is it me, did I run into a Dojo bug or is it a strange feature I just don't get? Any help is appreciated.

Greetings, Select0r

A: 

Try to connect not to the widget itself but to it's dom node:

dojo.connect(dataSelect.domNode, "onclick", function() {
        alert("clicked!");
      });
Kniganapolke
Unfortunately that doesn't work. In fact, no event at all is fired when connecting to the domnode.I've also tried to surround the select with a span and connect to onClick of that span, but with a similar result.
Select0r
Sorry, it's onclick not onClick. And get sure the domNode exists when you connect to it.
Kniganapolke
Thanks, I didn't now "onclick" and "onClick" would make a difference at this point.But unfortunately using "onclick" will result in the same behaviour I had before: the first click on the select doesn't fire the event (while the first click on the arrow does), and then the second click works.I see no reason why the first click would make a difference, maybe I should "activate" the select after it's rendered? Anyways, this more and more looks like a Dojo-bug to me...
Select0r
A: 

Select (which extends _HasDropDown) has fancy code to handle:

  1. mouse down on select widget
  2. mouse move to one of the options
  3. mouse up

Maybe that's canceling the click event.

Maybe you can connect to _loadChildren() instead.

Bill Keese
"_loadChildren" ist fired too early to be of use, as I want to load more children only when necessary.But "onMouseDown" (instead of "onClick") did the trick! Beats me why, but now the dropdowns behave exactly as intended.I still consider this being a Dojo-bug, but it's good to know how to get rid of it.Thanks very much!!
Select0r