iPhone Developer iPhone Developer - 1 month ago 5x
HTML Question

removing onClick from dynamic elements

Should I be removing dynamic clickHandlers to the dynamically created html tags or is it taken care of automatically by the garbage collector.

My primary browser is Safari (embedded in iOS app), but I think i read that IE has memory leak problem around this.

var li = document.createElement('li');
li.addEventListener('click', function(){});

so if this element was removed from DOM later on, should I delete the clickHandler, just in case please let me know how to property delete a clickHandler?

li.removeEventListener("click", function_name);

This will remove the event listener

Source: http://www.w3schools.com/jsref/met_element_removeeventlistener.asp

For anonymous functions, the point of them is really to have no reference and lacks a name, so for removeEventListener() you will need a named function.