Reputation: 3048
I've got 2 fields. One is a h:selectOneMenu with a list of places. the other is a h:inputText.
What i'm trying to do is, if the user selects place on the selectOneMenu, and clicks on search, it's gonna search for that place. If he selects a place on the selectOneMenu and still types someplace else on the inputText and clicks on search, it's gonna search for the place that was written and not the selected one.
So, it would be awesome if the user types anything on the inputtext, it would disable the selectOneMenu, and if he clear the field, it would be enabled again. I tried with javascript on event
onchange="if (this.value !='') document.getElementById('placeSelectOneMenu').disabled='true'"
but it didn't work. What can I do? Any ideas?
It only works when I click on search, than it reRender the field correctly. What I want is reRender dynamically.
Upvotes: 0
Views: 2485
Reputation: 1109532
The element ID needs to be the ID of the generated HTML element ID, not the JSF component ID. JavaScript has totally no notion of the server side JSF code. All it can see and access is just the HTML DOM tree as generated by JSF. Open the page in the browser, rightclick it and choose View Source. The generated HTML of a <h:selectOneMenu id="placeSelectOneMenu">
should look like this
<select id="someFormId:placeSelectOneMenu">
(where someFormId
is the ID of the parent <h:form>
)
In this case, you need to get it as follows instead
document.getElementById('someFormId:placeSelectOneMenu')
But since altering the HTML input elements on the client side without notifying the JSF on the server side can lead to unexpected behaviours (if you enable it on the client side by pure JS, then it won't be magically enabled on the server side as well and JSF would not process the submitted value of a field which was initially disabled) and that you're using RichFaces, I'd suggest to go for a pure JSF approach, for example with RichFaces 3.x's <a4j:support>
tag.
Upvotes: 2