I am trying to bind the click event to an element dynamically generated using the data with d3.js.
d3.select("#mylist").selectAll('li').data(data_arr).enter().append("li").html(
function(d)
{
var element = document.createElement('div');
element.innerHTML = '<div id="innerDiv"></div>';
var divToClick = element.querySelector('#innerDiv');
//Using jquery to get cross browser event binding
$(divToClick).click(function(){
alert('hello!');
});
return element.innerHTML;
});
I know that the problem is that I am returning an string (innerHTML) but if I return the element it doesnt work. Any help?
(The real code is much more complex, I just copied here the important part).
Here is the answer.