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 lik开发者_开发技巧e 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.
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.
精彩评论