Search code examples
javascripteventsdojohandler

Not working dojo/on or dojo/connect


I try use Dojo framework, but i can't use handlers(events) on href or some else element. Thanks.

 HTML:
 <a class="one" href="#">text_one</a>
 <a id="two" href="#">text_two</a>

 JS:
 require(
    ["dojo/query", "dojo/_base/connect", "dojo/on", "weather/handlers", "dojo/domReady!"],
    function(query, connect, on){
        var test = new weather.handlers;
        test.getCities();

        query(".one").forEach(function(node, index, nodelist){
            on(node, "onClick", function(evt){
                console.log("one clicked!");
            })
        });

        query('#two').on('onClick',function(){
            console.log('two clicked!');
        })
    }
);

Solution

  • The name of the DOM event you want to listen to is click, not onClick. Listening to onClick on a DOM element using dojo/on will do nothing.

    If you're going to be hooking up an event handler for multiple homogenous elements, you might also want to read about Event Delegation in the Events with Dojo tutorial.