Pierre GUILLAUME Pierre GUILLAUME - 7 months ago 14
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:

d3.selectAll("body")
.on('click', '.child', function(){
console.log("here");}


and later on,this code return the good elements :

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

Answer

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

$(document).on('click', '.child', function(){
     console.log("here");
});
Comments