Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Dojo have an equivalent to jQuery.trigger()?

In jQuery, you can do this:

$('#myElement').trigger('change');

How do I do that in Dojo?

like image 653
Nikki Erwin Ramirez Avatar asked Feb 15 '11 10:02

Nikki Erwin Ramirez


2 Answers

The dojo on.emit method (1.7+) can be used to trigger an event on a dom node. From the documentation page:

require(["dojo/on"], function(on){
    // register event handler
    on(target, "mouseup", function(e){
        // handle event
    });

    // Send event
    on.emit(target, "mouseup", {
        bubbles: true,
        cancelable: true
    });
});
like image 78
Steve Wranovsky Avatar answered Sep 20 '22 23:09

Steve Wranovsky


I don't think Dojo has similar functionality, at least as not as far as I know / can find. But you can use code like the following to replicate this functionality:

dojo.addOnLoad(function() {

    var button = dojo.byId("myButton");
    dojo.connect(button, "onclick", function() { alert("Clicked!"); });

    // IE does things differently
    if (dojo.isIE)
    {
        button.fireEvent("onclick");
    }
    else
    { // Not IE
        var event = document.createEvent("HTMLEvents");
        event.initEvent("click", false, true);
        console.debug(event);
        button.dispatchEvent(event);
    }
});

A little more verbose, for sure, but you would be able to create your own Dojo version of trigger() with it.

Try it out

like image 33
GreenWebDev Avatar answered Sep 20 '22 23:09

GreenWebDev