最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Removing the event listener on a button programatically - Stack Overflow

programmeradmin5浏览0评论

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?

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?

Share Improve this question edited Jan 8, 2023 at 13:15 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Apr 3, 2011 at 11:10 KiranKiran 1151 gold badge3 silver badges8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 14

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/gaby/PBjtZ/

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.

发布评论

评论列表(0)

  1. 暂无评论