Adding event listener to dynamicly loaded content

40 views
Skip to first unread message

Mateusz Lewandowski

unread,
Sep 25, 2016, 4:46:23 PM9/25/16
to Dart Misc
Hi.
i have a problem with add on click event listener on dynamicly loaded html ( on button click)
When i am trying to listen on click event the error occurs.
"Uncaught Unhandled exception:
The null object does not have a getter 'onClick'." 

I have the following

Logon logon=new Logon();

DivElement div = querySelector('#MainContent');

EventListener HandleEvent(event)// async
{

switch(event.detail) {
case 'Logowanie':
{
HttpRequest.getString("logon.html").then((resp) {
div.append(new Element.html(resp));
});
div.querySelector("#login").onClick.listen((e) {
print("Loading...");
logon.LoginMethod();
});

break;
}
}
} main() async{

document.addEventListener('todochange',HandleEvent);
...
}


I have tried different ways with no result.
Please help me with this.

Jan Mostert

unread,
Sep 26, 2016, 2:28:59 AM9/26/16
to Dart Misc
You need to add the click listener in the callback after your HTML is loaded.
Otherwise the HTML has not been loaded into the div yet.


--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.

Jan Mostert

unread,
Sep 26, 2016, 2:30:58 AM9/26/16
to Dart Misc
I believe this should fix it ?        

        HttpRequest.getString("logon.html").then((resp) {
div.append(new Element.html(resp));
          div.querySelector("#login").onClick.listen((e) {

print("Loading...");
logon.LoginMethod();
});
        });

Mateusz Lewandowski

unread,
Sep 26, 2016, 2:53:32 PM9/26/16
to mi...@dartlang.org
It works.
Thanks a lot
Reply all
Reply to author
Forward
0 new messages