AppendChild at XUL Firefox Addon Break

I am working on a Firefox addon and now I need to dynamically add menu items to the menupopup element. I tried basically all approaches to the Mozilla Developer Center, and none of them work.

    function populateDropdown() {
    var counter = 0;
    for (var key in services) {
        var newMenuItem = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "menuitem");
        newMenuItem.setAttribute("label", services[key]['title'])

        document.getElementById("mainDropdown").appendChild(newMenuItem);
    }
}

This code breaks into the appendChild command. Any ideas why?

+5
source share
1 answer

Are you 100% sure that document.getElementById ("mainDropdown") returns a nonzero result?

Try breaking it into pieces and adding code for debugging:

var dropDown = document.getElementById("mainDropdown");
if(dropDown) {
  alert("dropDown found!");
  dropDown.appendChild(newMenuItem);
}
+5
source

All Articles