Pierre GUILLAUME Pierre GUILLAUME - 2 years ago 102
Javascript Question

How to use "on click" before the creation of my element

I try to trigger an event on user click, then I used the on click method of JQuery. The problem is that I create my element after my onclick function.
I've read here or here that we can add a balise name between click and the function but my code return

n.apply is not a function

Here is my JQuery method:

.on('click', '.child', function(){

and later on,this code return the good elements :

console.log(d3.selectAll("body .child"));

Answer Source

Try this : use $(document) as parent element to delegate click event calls

$(document).on('click', '.child', function(){
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download