Re[2]: Assign a context menu to a dynamically created HTML eleme
2005/02/03 16:14
Viewed 10241 times
Replies: 1/4
by cclc

Thanks a lot for your prompt reply. Actually I thought on the solution you suggested, but I really don't like to call the "setup" method each time a new element is created, especially when there are a lot of HTML elements in the document that need to be parsed (I imagine a large tree, dynamic, with 2000-3000 nodes, each node having a context menu assigned). I'm thinking on another solution: creating a single element in the document, hidden, standard assigning the context menu using "setup" and, for all dynamic created elements of that type, treat "oncontextmenu" and "onclick" handlers through the similar handlers in the hidden element:
<!--
triggerCtxMenu = function(e, someInfo) {
//do something with "someInfo" like storing it in a global variable for further reference
getElementById("myhiddenelement").onclick(e);
}
<mydynamicelement onclick="triggerCtxMenu(event, 'some info about the element');" >
-->
What do you think about it?

Re[3]: Assign a context menu to a dynamically created HTML eleme
2005/02/03 20:53
Viewed 12099 times
Replies: 1/3

You could try the idea but I'm not sure you will be able to determine the element that was actually clicked on in your handler.

last
Re[4]: Assign a context menu to a dynamically created HTML eleme
2005/02/04 09:06
Viewed 13884 times
Replies: 1/2
by cclc

I tried something like that:
document.write('....' + ' oncontextmenu="triggerCtxMenu(event, \'' + node.name + '\');" onclick="triggerCtxMenu(event, \'' + node.name + '\')"';

where:

triggerCtxMenu = function(e, nodeInfo) {
getElById("options").innerHTML = nodeInfo;
getElById("options").onclick(e);
}

and the hidden element having the ID "options" is a DIV, "nodeInfo" is the information related to the element that triggered the menu (node.name):
<div id="options" class="options dynarch-menu-ctxbutton-both" style="display:none"></div>

(Note: "getElById" is an utility function for "document.getElementById")

The handler has the following implementation:
function onClickContextMenu(action) {
var nodeInfo = action.info.menu.target.innerHTML;

switch (action.params.id) {
case "operation1" :
alert('op1:' + nodeInfo);
break;
case "operation2" :
alert('op2:' + nodeInfo);
break;
case "operation3" :
alert('op3:' + nodeInfo);
break;
}
}

I haven't tried yet to pass the object reference to the hidden element (as an attribute)...

last
Google