It's not finding your component. Since your <h:inputText>
is inside of an <h:form>
the id
will have the following pattern
formName:componentName
. Since you did not specify an id
for <h:form>
, JSF will generate one for you. That's why you are seeing j_id1926454887_72d35e53:country
where j_id1926454887_72d35e53
is the form id
. If you want to deal with simpler id
, then you should add an id
value to your <h:form>
. For example
<h:form id="form">
<h:inputText id="country" value="#{country.localeCode}" size="20" />
</h:form>
The id for <h:inputText>
will now be form:country
.
Even simpler, you could simply add prependId = "false"
to your form
<h:form prependId="false">
and now your id
for <h:inputText>
will simply be country
.
How can I get this id dynamically?
I'm going to modify your code slightly to achieve what you want (I'm thinking that you want to disable the input
based on a specific event). Consider this simpler example
<h:form>
<h:inputText id="country" value="#{country.localeCode}" size="20" onclick="disableInputText(this.form)" />
</h:form>
Here I'm attaching a javascript function to an HTML DOM Event handler. In this case, I want the input
to be disabled when I click on it (hence onclick
). I'm also passing the form
as a reference in the function. Then, define your Javascript like this.
<script>
function disableInputText(form) {
form[form.id + ":country"].disabled = true;
}
</script>
We simply grab the input
in the javascript with the corresponding id
via the object form
and set its disabled
attribute to true
. (You can sort of view the form
object as Map
).
Also check the link below for more attributes of <h:inputText>
and the different handlers you can use (the ones with on
as prefix).
inputText Tag Attribute.
Also, check out the answer with the most votes to get a bigger picture on how ids are generated and other ways on how they can be determined.
How can I know the id of a JSF component so I can use in Javascript