views:

843

answers:

4

I'm at my wits end, and it's probably something really simple.. But I've got basically this code:

var menuitem = document.getElementById('mymenuitem');
alert(menuitem);
alert(varImChecking == null);
menuitem.setAttribute('disabled', (varImChecking == null) ? 'true' : 'false');

It should be disabling the 'mymenuitem' menu item, but has no effect. When run, the alerts say '[Object XulElement]' followed by 'true'. I know the getElementById is selecting the correct menuitem element because onclick bindings to the menuitem variable work.

Some alternatives I've tried:

menuitem.setAttribute('disabled', (varImChecking == null));
menuitem.disabled = (varImChecking == null);

So why isn't the setAttribute having any effect? It doesn't even gray out the menu item. Is there another way I'm supposed to do this?

Edit: Forgot to mention, no warnings or errors show up in the console.

A: 

may be this will work:

var menuitem = document.getElementById('mymenuitem');
alert(menuitem);
alert(varImChecking == null);
menuitem.setAttribute('disabled', (varImChecking == null) ? true : false);

I used true and false rather than 'true' and 'false'

Marwan Aouida
Thanks for the suggestion, but sadly, it's still giving me the same result.
mrdrbob
A: 

I believe that setAttribute will be looking for a string "true"/"false", as you originally had.

Although I don't see a bug immediately in why you can't disable. For a test, have you tried simply menuitem.setAttribute('disabled', 'true'); ? If that doesn't work - perhaps nothing will. It seems odd to disable a menuitem - are you sure you're not wanting to disable the menulist?

I do see a problem with your enabling code. I can't find great documentation for it, but I did deal with this previously. One example you can find in bugzilla is the fact that setting disabled=false actually disables items on Linux. The recommended way from Mozilla is to remove the attribute when you want it enabled.

So although your solution is more concise, you should probably look at something more like this:

var menuitem = document.getElementById('mymenuitem');
alert(menuitem);
alert(varImChecking == null);
if(null==varImChecking){
   menuitem.setAttribute('disabled', 'true');
}else{
   menuitem.removeAttribute('disabled');
}
pc1oad1etter
Thanks for the information. I'm just disabling an item in a context menu in an addon I'm creating, similar to how back, forward, etc are disabled when you right-click a webpage, but have no history in Firefox. I'm kind of a XUL n00b, so if there's another way I should be doing this, I'd be happy to hear it.
mrdrbob
+1  A: 

I think I finally figured this out. The problem is that I had the menuitem I was trying to edit was hardcoded inside a .xul file. For whatever reason, this made it impossible to change the disabled or hidden attributes.

So I removed the menuitem from the .xul file and dynamically created it in the javascript instead. Now it works exactly as expected:

var menuitem = document.getElementById('mypopupitem').appendChild(document.createElement('menuitem'));
menuitem.setAttribute('disabled', 'true'); // Works.
menuitem.removeAttribute('disabled'); // Also works.

Does anyone know, is this a feature/quirk of XUL, or am I just doing it wrong?

UPDATE: As many comments have pointed out, the above is not correct. You absolutely can change the disabled attribute on an item hard coded in a XUL file.

After much more debugging, paring the problem down to its core elements, pulling hair, etc, it turned out to be.... a typo. Grrr.. Sorry.

It turns out my document.getElementById('menuitem') was grabbing the XUL element, not the element. The fact that the onclick method seemed to be firing correctly seems to have been thanks to event bubbling (I guess?). I still have no idea why setAttribute('label', 'changed') worked when I tried that, but unfortunately, in the frenzy of debugging, I don't have that version to check again.

Anyway, it's working now, without having to dynamically create the menuitem. Thanks for all the help!

mrdrbob
this is stange, it should work even if the menuitem is hardcoded in the xul file
Marwan Aouida
I agree. mrdrbob, perhaps you could post the xul that you were working with? Glad to hear this is working for you, but you definitely should *not* have to add it by manipulating the DOM.
pc1oad1etter
Is the menuitem in a menu, a menupopup, or a menubutton? Do you have it inside a menupopup object inside whatever parent you're using?
Joel Anair
A: 

I am using tag to add a custom context menu. When I use "onpopupshowing" event to handle the enable/disable of a menu-item, my context menu is getting populated with a lot of menu items which I have not intended to add. Here is the new popup tag that I added in the overlay.xul

<popup onpopupshowing="isAssertTextEnabled(this)" id="contentAreaContextMenu">
<menuitem id="context-recorderpopup" label="Assert text present"
accesskey="T"
insertafter="context-stop" oncommand="onAssertionMenu(event)" />
</popup>

please let me know if I am missing out something

Thanks, Vinay