hulapalu hulapalu - 25 days ago 6
HTML Question

Adding onclick event to input field programmatically in javascript doesn't work

I want my

Input
field to look like this:

<input type="submit" value="Remove" onclick="deleteRow(this)">


But it's not working. I've tried to:

Input.onclick = function () { deleteRow(this); };


Doesn't work as well. My deleteRow function looks like:

function deleteRow(o) {
var p = o.parentNode.parentNode;
p.parentNode.removeChild(p);
}


Any suggestions on how to make it work?

Answer

You can add an id to your input field and attach the onclick event to it:

function DeleteRow(o) {
  var p = o.parentNode.parentNode;
  p.parentNode.removeChild(p);
}
document.getElementById("myinput").onclick = function () {
  DeleteRow(this);
};
<input type="submit" id="myinput" value="Remove" onclick="DeleteRow(this)">