View Full Version : detachEvent using removeElement

12-14-2005, 09:40 PM
So i'm kind of making a dynamic menu based off of a user right clicking. Depending on where the click occurs i want different menu options to appear or even be disabled (no onmousedown event). I'm creating all of the menu options dynamically using createElement and attachEvent for the onmousedown event.

since i'm using anonymous functions, i can't directly remove them usin detachEvent.

var menu = document.getElementById('menu');
var item = document.createElement('DIV);
item.innerHTML = 'menu item!';
item.attachEvent('onmousedown', function() { alert('blah'); } );

the following will not work...

item.detachEvent('onmousedown', function() { alert('blah'); });

the question is... if i keep dynamically creating and removing elements from the menu, as opposed to trying to detach and retach events... would that be a better memory managment model? I guess I'm just worried about memory leaks without being able to detach any of these anonymous functions. Or are there any other suggestions as to how i might accomplish having different menu options when different elements are right clicked?

a quick example of my code can be found at...

P.S. you'll have to goto view->source code since the right click is disabled :)