erotavlas
erotavlas

Reputation: 4483

How to determine which widget was clicked? Dojo

Is there a way to observe the mouse click event and determine which widget was clicked?

So basically I wish I could do something like this (on mouse click anywhere on the page)

on("click", function (e) {
   //var aWidget = dijit.getEnclosingWidget(e.target);
   //var id = aWidget.id
   //do something based on the widget id
});

Upvotes: 0

Views: 381

Answers (2)

ben
ben

Reputation: 3568

You can but it will requires some extra steps.
Basically, the logic is: - When you click on a node, you have to go up in the DOM until you find a node with the attribute widgetId - When you have it, use dijit/registry::byNode to fetch the widget.

If you skip the node traversing, you will find the widget only if the main domNode of the widget was clicked.

require(['dojo/on', 'dijit/registry'], function(on, registry){
    on(document, 'click', function(event){
        var target = event.target,
            widget;
        while(!target.getAttribute('widgetId') && target.parentNode) {
            target = target.parentNode;
        }
        widget = registry.byNode(target);
        console.warn(widget);
    });
});

Be aware, this method will work ONLY if you have 1 dojo instance loaded on in the page.
If you page have more than one dojo instance then the document click event must be attached by every dojo instances.

Upvotes: 1

tik27
tik27

Reputation: 2698

Might be a lot of overhead but yes you can

require(["dojox/mobile/deviceTheme","dojo/on","dijit/registry"],function(theme,on,reg){
    on(document,'click', function(e){
    console.log(e.target);
    var widget = reg.byNode(e.target);
    console.log("foundWidget::" + widget.id);
  });
});

Upvotes: 1

Related Questions