Reputation: 6812
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.
Upvotes: 0
Views: 1940
Reputation: 1365
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>
Upvotes: 0
Reputation: 165059
document.createElement()
this
context#myView
element of all child nodes#myView
elementMyEditor.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)
}
Upvotes: 3
Reputation: 138537
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);
}
Upvotes: 3