File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes Change disabled attribute on client side Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Change disabled attribute on client side" Watch "Change disabled attribute on client side" New topic

Change disabled attribute on client side

Tony David

Joined: Mar 19, 2012
Posts: 15
Hi everybody

When a JSF component is initialized with the attribute disabled set to true, if we changed it on the client side with javascript code setting it to true, the value is not submitted.

If I changed the value of my component test, the text zone is enabled, but the value is not sent to the managed bean, so I suppose JSF checks its component tree before setting values, and this component was initially disabled that's why the value is not submitted.

What do you suggest to get the value on server side ?
I can get it in the request but I would like to know if we have a way to avoid that.

Thanks for your help
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17410

The only thing I can think of more likely to give you problems than using Javascript to alter the "disabled" attribute client-side is using Javascript to alter the "rendered" attribute client-side. In both cases, you can expect the JSF HTML renderer to produce seriously different HTML depending on which state these attributes were set to in the original template. In the case of "disabled", that probably includes extra form data that's not actually in - or even physically near - the disabled control itself.

The safest way to do this is to manage the disabled state via an EL reference to a server property. Which means at least a partial page refresh (AJAX) is needed to update the UI. An alternative is to code the control enabled and disable it under Javascript control. That way, you'll have the form metadata available when you re-enable it. This isn't totally safe, but short of getting the disable status from the server, it's about the best you can do.

An IDE is no substitute for an Intelligent Developer.
Tony David

Joined: Mar 19, 2012
Posts: 15
Thanks for you help,
indeed, if the component is initialized with disabled set to true, it will not be in the JSF tree, so matching values will not be submitted.

As you suggest two ways to avoid that :

- Manage it with javascript by adding a JS script to disable the component just after the component declaration

- If we prefer to manage it completely with JSF, when we want to switch the disable state, we must trigger an Ajax event which will reassess the EL expression set on disabled attribute of the inputText.

If the component was set to disabled, so not present in the jsf tree, during the ajax request to enable it, it will be added, so the value may be submitted later
This solution will cost a request on the server..., just to change an element state

The perfect solution should be to manage the disable state with JSF and WITHOUT such an Ajax request but, I don't know why JSF specifies to not introduce disabled element in the JSF tree, because in my opinion
disabled during initialization phase does not mean disabled for all the component life.
I agree. Here's the link:
subject: Change disabled attribute on client side
jQuery in Action, 3rd edition