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?

Answers:

Answer

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/

Answer
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.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.