Re[3]: Assign a context menu to a dynamically created HTML eleme
2005/02/03 20:53
Viewed 12780 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.

Re[4]: Assign a context menu to a dynamically created HTML eleme
2005/02/04 09:06
Viewed 14671 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
Re[5]: Assign a context menu to a dynamically created HTML eleme
2005/02/04 14:31
Viewed 16709 times
Replies: 1/1

I analyzed your problem in more detail today.  I'm sorry it took so long for the right solution...  Now, here we go.  Define the menu like this:

<ul id="menu">
<li id="context-menu">
The label doesn't matter
<ul>
<li>... context menu</li>
<li>... items</li>
</ul>
</li>
</ul>

So note that I assigned an ID to the LI which holds the context menu contents.  Now, remember a reference to the DynarchMenu object when creating it:

var menu = DynarchMenu.setup("menu", { context: true });

Now we need to create a dynamic element and assign the context menu to it:

var div = document.createElement("div");
...
document.body.appendChild(div);
// this does it:
DynarchMenu.setupContext(div, menu.items["context-menu"]);

So there is actually an API for that :-)  I'm just sorry I forgot about it in the first place, I could have helped you faster.  To the “setupContext” function you need to pass a reference to the HTML element that should get a context menu and the item that holds the context menu--as you can see we can easily retrieve that using the “menu.items” hash, assuming we gave it an ID and have kept a reference to the DynarchMenu object.

Hope this helps.

 
last
Google