Kiran
Kiran

Reputation: 115

Removing the event listener on a button programatically

I have a button which is registered with a onclick event as shown

<Input type="Button" name="Register" Value="Register" onclick="CallMe();"/>

Is it possible to programatically remove or deregister the onclick event on this button?

Upvotes: 5

Views: 11442

Answers (2)

Gabriele Petrioli
Gabriele Petrioli

Reputation: 195972

You could set the onclick attribute to null.

var el = document.getElementById('inputId');
el.onclick = null;

or better just remove the attribute altogether with the removeAttribute() docs method of the element.

var el = document.getElementById('inputId');
el.removeAttribute('onclick');

you will need to add an id to the element though for this..

example code at http://jsfiddle.net/gaby/PBjtZ/

Upvotes: 14

meo
meo

Reputation: 31249

document.getElementsByName("Register")[0].onclick = "" 

or

document.getElementsByName("Register")[0].removeAttribute("onclick")

Make sure to place this in a JS tag at the end of your document. So the DOM is available when this script is running.

the first example gets all elements with the name "Register" in your dom and returns the first, then it finds and sets the onclick attribute to an empty string. (could be null to)

the second example does the same, but removes the attribute "onclick".

Be careful if you have more then one element with the name "Register" you should do it like Gaby aka G. Petrioli told you to.

Upvotes: 1

Related Questions