/    Sign up×
Community /Pin to ProfileBookmark

Menu Event Listener

Hi,

I’m dynamically populating a menu with menuitems and I’m looking to add an even listener to each menu item as it generated.
By doing this, each menu item when clicked, will be added to a textbox.

I know my code for adding to the textbox works, but I can’t seem to get the generated items to work, I think its something to do with me trying to access a ‘menuitem’ which are only generated when the user takes a certain action. Does anyone know how to access generated menuitems?

Generating the menu items

[code]
var menu = document.getElementById(“tagmenu”);

for (var j = 0; j < x.length; j++)
{

t = x[j].getAttribute(“content”);
var temp = document.createElement(“menuitem”); // create a new XUL menuitem
temp.setAttribute(“label”, t);
temp.setAttribute(“type”, “checkbox”);
menu.appendChild(temp);
temp.addEventListener(“command”, addTags, false);

}

[/code]

Adding generated items to textbox

[code]
function addTags()
{
var x= document.getElementById(“menuitem”);
y = x.getAttribute(“label”);
if (x.checked = true)
{
var t = document.getElementById(“get-tag”);
t.setAttribute(“value”, y);

}
}

[/code]

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @Hysteria86 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...