» Forums
» DynarchMenu forum
» Support
» Assign a context menu to a dynamically created HTML element
| Assign a context menu to a dynamically created HTML element ¶ | |
| 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 ¶ | |
| 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 ¶ | |
| 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: | |
| last |
(*) Discounts available for the above products!













