简体   繁体   中英

Call prototype function from onclick event, when button is added dynamically

I have two prototype functions showPopup and buildView . In buildView , I am generating a dynamic HTML with a button and would like to call showPoup when button is clicked. However, when button is clicked, it throws

Error: The value of the property 'showPopup' is null or undefined, not a Function object

MyEditor.prototype.buildView = function(id) {
   document.getElementById("myView").innerHTML = "<input type='button' value='Edit' onclick='showPopup(getName(id), getPlace(id))' />";
}

MyEditor.prototype.showPopup = function(name, place) { }

I even tried using onclick='MyEditor.showPopup(getName(id), getPlace(id))' , didn't work either.

  1. Create your element via document.createElement()
  2. Attach a click event listener to the element using an arrow function to preserve the this context
  3. Empty out the #myView element of all child nodes
  4. Append the element to your #myView element
MyEditor.prototype.buildView = function(id) {
  const btn = document.createElement('input')
  btn.type = 'button';
  btn.value = 'Edit'
  btn.addEventListener('click', () => {
    this.showPopup(getName(id), getPlace(id))
  }, false)

  // empty out #myView
  const view = document.getElementById('myView')
  while (view.firstChild) {
    view.removeChild(view.firstChild)
  }
  view.appendChild(btn)
}

Because you have to call this.showPopup() , which is only possible if you build up the DOM manually:

 MyEditor.prototype.buildView = function(id) {
    const button = document.createElement("input");
    button.type = "button";
    button.value = "Edit";
    button.onclick = (evt) => {
      this.showPopup(getName(id), getPlace(id));
    };

     document.getElementById("myView").appendChild(button);
  }

If you want to build element by html text, you need do something like this.

 MyEditor = function() {}; MyEditor.prototype.buildView = function(id) { document.getElementById("myView").innerHTML = "<input type='button' value='Edit' onclick='editor.showPopup(\\"name\\", \\"id\\")' />"; } MyEditor.prototype.showPopup = function(name, place) { console.log(name) } var editor = new MyEditor() editor.buildView(); 
 <!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"></head> <body> <div id="myView"></div> </body> </html> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM