views:

444

answers:

2

Hi all

I have some code in which I am creating an dijit.Dialog with an Ajax call(href property) to populate the contents.

I what to parse the returned content to dojo.connect a click event to the links in the returned content.

But when I do a dojo.query("a",this.current_popup.domNode) I get an empty array back

Does anybody know how can get can attach a onclick to all the returned content?

Paul

+2  A: 

It is less expensive to just bind an event handler to the DOM node of the Dialog widget, and use event delegation to capture any "a" clicks. This way, you can avoid any event handler cleanup, particularly if the contents of the Dialog change frequently. You can avoid the event handler cleanup if you use the widget's connect method to do the work.

So, if you are doing the connect inside a method in the dijit.Dialog, you could use something like:

this.connect("onclick", function(evt){
    var node = evt.target;
    if("a" == node.nodeName.toLowerCase()){
        //node is an a tag, do what you want with it,
        //for example, read node.href to get the URL attached to it.
        //If you want to prevent following that URL and prevent further
        //event bubbling, stop the event:
        dojo.stopEvent(evt);
    }
});

If you are doing this connection work outside the widget instance, instead of using this.connect, use widgetInstance.connect(), assuming widgetInstance is a variable that refers to the dijit.Dialog instance.

By using that version of connect, the widget will automatically unregister the event handler when the Dialog widget is destroyed, keeping the memory profile in check.

jrburke
I like the feel of this method I assume that I can add a class test to this as well to control which Href that the click is actioned on.
Pbearne
A: 

One way I found to do this was to add a delayed connect to the widget

dojo.connect(this.current_popup, "onDownloadEnd", function(){
      dojo.query("a.popup",this.current_popup).forEach(function(node) {
          // add your function add here

        });
});

This runs after the ajax call is complete so you can now find the objects in the DOM

Pbearne