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
  • 5,676
  • 16
  • 65
  • 141
zippax
  • 262
  • 1
  • 5
  • 13
  • @ A. El-zahaby your code also work, thanks ^^ – zippax Mar 09 '19 at 23:15