Mohamad Yassine
Mohamad Yassine

Reputation: 62

How can I make a data-dojo-attach-event run?

I am trying to run the event after adding a domNode programatically as such:

<a href="javascript:void(0)" data-dojo-attach-event="click:openRegistration">Register</a>

This event is not parsed by Dojo when the page is first loaded because it is being added later on. Even after running

parser.parse();

The event does not run. How can I make this event run?

Upvotes: 1

Views: 3729

Answers (3)

Kyle Gilbertson
Kyle Gilbertson

Reputation: 302

You don't want the dojo parser to parse the page twice: it would redundantly parse and create things that were already created and cause a mess. To add a node programmatically after the page has been parsed, have a look at dojo/dom-construct.

require(["dojo/dom-construct", "dojo/domReady!"],
    function(domConstruct) {

        var openRegistration = function() {
            alert("foo");
        }

        domConstruct.create("a", {
            innerHTML: "Register",
            click: openRegistration,
            href: "javascript:void(0)"
        }, document.body);
    });

Replace document.body with a reference to the parent node where you want to insert the node, and look at the 3rd parameter for placement options.

Upvotes: 2

denov
denov

Reputation: 12726

Without seeing the rest of you code I'm gussesin you have a scope issue. Or you haven't cased the dom event correctly - onClick

You function needs to be part of the same widget that uses the templates with attach-events that inherits _TemplatedMixin. You widget that's using that attach-event should look something like

require([
    'dojo/declare',
    'dijit/_WidgetBase', 
    'dijit/_TemplatedMixin', 
    'dojo/text!myNameSpace/templates/MyWidget.html'
], 
function(declare, _WidgetBase, _TemplatedMixin, template){
    declare('MyWidget', [ _WidgetBase, _TemplatedMixin ], {
        templateString: template, 
        openRegistration: function {
               // do stuff here
        }
    });
});

Upvotes: 1

GibboK
GibboK

Reputation: 73988

You should use onclick:openRegistration instead of click:openRegistration.

<a href="javascript:void(0)" data-dojo-attach-event="onclick:openRegistration">Register</a>

Upvotes: 1

Related Questions