lwconquer
lwconquer

Reputation: 865

How to subscribe an dijit event in the JS code?

I am new to the Dojo framework, and try to subscribe an onChange event for a ComboBox Dijit in the code grammatically. But for some reason, my event handler function can't catch this event. Could you help me to find out where am I wrong?

    <div id="container" style="padding: 5px;">
        <select data-dojo-type="dijit/form/ComboBox"     name="state" id="stateInput">                
            <option>Apples</option>
            <option selected>Oranges</option>
            <option>Pears</option>
        </select>           
    </div>




    require([   'dijit/form/ComboBox',  
        "dojo/on",
        "dijit/registry"    
    ], function(ComboBox, on,registry){
        var comboBoxDigit2 = registry.byId("stateInput");
        on(comboBoxDigit2,"onChange", function (e) {
            alert("I am an alert box!");
    });    
});

Code is here on jsfiddle.

Thanks,

Wei

Upvotes: 0

Views: 64

Answers (1)

Matt R
Matt R

Reputation: 734

There are a few errors in this code.

  1. require passes the array to function in the same order. You are currently passing "dojo/on" and "dijit/registry" into ComboBox, on, and registry. You need to require "dijit/form/ComboBox"

  2. The format for data-dojo-type should be a path ie data-dojo-type="dijit/form/ComboBox"

  3. The format for on is typically on(element, eventname, function) although I believe you can do it the way you are doing it.

Edit:

on works with the event ie

on(combobox, "change", function(value) {...});

You original fiddle was not being parsed as a dijit, it was just a plain html select, so the call to registry was failing. I've updated the fiddle. http://jsfiddle.net/vdmhU/13/

Upvotes: 2

Related Questions