jonnyhitek
jonnyhitek

Reputation: 1561

Problem connecting an event to dynamically added dom element with dojo

I am having a problem with connecting a click event to a dynamically added dom element. I add a new dom element like this:

var link = dojo.create("a",{
                      className: "deleteDnd",
                      innerHTML: "Delete"
                    },this.domNode,"first");

I would now like to add a click event to this dynamically added link, however I have tried to add the event numerous times but it fails. I am able to connect the event using firebug after the element has been appended. Is there a way to connect the event whilst creating the element something like :

var link = dojo.create("a",{
                      className: "deleteDnd",
                      innerHTML: "Delete"
                    },this.domNode,"first");
dojo.event.connect(link, "onclick", function(node, index, nodelist){
                        console.log("was clicked");

                    });

Any help would be greatly appreciated.

Upvotes: 1

Views: 1121

Answers (1)

Frode
Frode

Reputation: 5710

The function dojo.event.connect doesn't exist anymore in the newer versions of Dojo. Try using just dojo.connect instead.

dojo.connect(link, "onclick", function(node, index, nodelist){
        console.log("was clicked");
        console.log(node, index, nodelist); // Only first argument is defined
    });

You'll also notice that the link's event handler function will only get one argument (the event), not node, index and nodeList.

Upvotes: 1

Related Questions