faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

10 of 13 people (77%) answered Yes
Recently 4 of 7 people (57%) answered Yes


How do I unassign event types from objects? Basically, I need to free up as much memory as possible when many elements are assigned an event type.

Apr 4th, 2008 20:00
ha mo, Alex Fenton, Guest,

If I've understood the question correctly, what you want to do is remove
event handlers from an object. To do this, set the object's
relevant event handler property (onclick, onsubmit, onfocus, onmouseover
etc) to be the special value 'null'. This removes any Javascript
handling of that event type from that element.
For example, when the link in the script below is first clicked, it
removes its own onclick event handler, so when you click the link again,
it functions 'normally', as if it had no handler.
<SCRIPT LANGUAGE="Javascript">
function disableHandler (evt_target) {
	window.alert('disabling handler');
	evt_target.onclick = null;
	return false;
<A HREF="wherever.html" ONCLICK="return disableHandler(this)">Do Me</A>
If you wanted to remove large numbers of event handlers at once, you
could iterate through any useful array of elements - document.forms,
document.all, whatever.