2

I need to call a function inside innerHTML and this is my try but it doesn't work.

category.find({}, function(err, docs) {
    docs.forEach(function(d) {
          var id = d._id
          document.getElementById('result').innerHTML += '<li onclick=insert(id)>' + d.name + '</li>'
    })
})

function insert(id){
  alert(id + "Inserted")
}

Here is the HTML Part:

<div id="result"></div>
demo
  • 6,038
  • 19
  • 75
  • 149
zippax
  • 284
  • 2
  • 5
  • 13
  • @ A. El-zahaby your code also work, thanks ^^ – zippax Mar 09 '19 at 23:15