2

I am trying to add tree button items from my database table to my drop down menu, but the onclick event doesnt call the function being passed. Here is the error I am getting upon clicking the button. Any help would be appreciated.

addFormatItemHandler() 
{
    var self = this;

    this.m_tree.formatItem.addHandler
    (
        function(p_sender, p_event) 
        {
            var item = p_event.dataItem;

                    p_event.element.innerHTML = "<button onclick='myFunction()'></button>";                                 

        }
    );

}

myFunction(){

console.log("My function");

}

Expected result: My function

Actual result: myFunction is not defined at HTMLButtonElement.onclick

Faisal Mehboob
  • 609
  • 7
  • 17

1 Answers1

0

As long as you put the word function before your function it should work if you're doing it like the following example:

function addFormatItemHandler() {
  var self = this;

  this.m_tree.formatItem.addHandler(
    function(p_sender, p_event) {
      var item = p_event.dataItem;
      p_event.element.innerHTML = "<button onclick='myFunction()'></button>";
    }
  );
}

function myFunction() {
  console.log("My function");
}
<button onclick="myFunction()">My button</button>
Pietro Nadalini
  • 1,722
  • 3
  • 13
  • 32