I'm working with a user script that creates a menu of clickable links for the user. These entries are stored into an array initially and then the array is iterated over and the functions are added to the .click callback.
This is how the menu is built up:
registerMenuCommand('Options', function() { DisplaySlideMenu(true); });
function registerMenuCommand( oText, oFunc ) {
falsifiedMenuCom[falsifiedMenuCom.length] = [oText,oFunc];
}
I can successfully add and execute the menu commands using standard javascript and code like the following:
for( var i = 0; GM_falsifiedMenuCom[i]; i++) {
var menuEntry;
mdiv.appendChild(menuEntry = document.createElement('a'));
menuEntry.setAttribute('href','#');
menuEntry.onclick = new Function('falsifiedMenuCom['+i+'][1](arguments[0]); var e = arguments[0]; e.stopPropagation(); return false;');
menuEntry.appendChild(document.createTextNode(falsifiedMenuCom[i][0]));
}
but I decided to try and convert it to jQuery (so that things looked a little prettier) which I made look like this:
for( var i = 0; falsifiedMenuCom[i]; i++) {
var mEntry = $('<a href="#">' + falsifiedMenuCom[i][0] + '</a>');
mEntry.click(function () { debugger; falsifiedMenuCom[i][1](arguments[0]); var e = arguments[0]; e.stopPropagation(); return false; });
mdiv.append(mEntry);
}
And of course it doesn't work. Everything gets added correctly to the menu, but when I click on one of the entries the .click gets called and I get an error Uncaught TypeError: Cannot call method '1' of undefined
. This makes sense because the .click function for all the menus is falsifiedMenuCom[i]1 and i == falsifiedMenuCom.length which is past the end of the array. I understand why all that is, but is there anyway to get the .click function to actually take the actual array value of falsifiedMenuCom[i][1] and NOT just attempt to call falsifiedMenuCom[i][1] itself? Should i just be sticking with the plain old javascript?