» Forums
» DynarchMenu forum
» Support
» Assign a context menu to a dynamically created HTML element
Assign a context menu to a dynamically created HTML element
2005/02/03 11:20
Viewed 6124 times
Replies: 1/6
by cclc

Hi

I have the following situation: I have a kind of HTML grid that is a table where the user can add or remove rows by pressing some icons associated to each row. I would like to replace the icons with a context menu containing the add/remove row operations. The problem is this one: it's not possible to use DynarchMenu.setup method to assign the context menu to each table row because they are created on user action (using JavaScript), so treating the document onload does affect only the initial rows of the table. I tried to call DynarchMenu.setup after each row creation, but it seems the <ul> element containing the menu definition is not anymore in the current document (it was removed by the DynarchMenu.setup called after the docuement was loaded). Then I proceeded to inspect the DOM structure of the document trying to find what "changes" were perfomed over the elements associated with the context menu. I found oncontextmenu, onclick and __msh_info2 properties assigned to the elements I was interested in. Then it was no problem to assign the context menu to the newly created elements by simply copying the 3 properties from the initially created elements. But I don't like this solution and I'm asking if there is no other way (an API) to "assign" a context menu to a dynamically created HTML element.

Thanks in advance!

Re: Assign a context menu to a dynamically created HTML element
2005/02/03 15:24
Viewed 8222 times
Replies: 1/5

Well, for now the solution that you found is best. There's another possible workaround--clone the UL element that defines the menu before calling "DynarchMenu.setup(...)" and you can reuse it later. You can clone it with, say, "var clone = document.getElementById("menu").clone(true)" and on subsequent requests call "DynarchMenu.setup(clone)".

We will create a simpler API in the future.

last
Re[2]: Assign a context menu to a dynamically created HTML eleme
2005/02/03 16:14
Viewed 10238 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?

last
Google