hulapalu hulapalu - 1 year ago 64
HTML Question

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

I want my

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;

Any suggestions on how to make it work?

Answer Source

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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download