These are the steps taken by JSF life cycle:
1) if the drop down menu changes, selectedOptionChanged will get called.
2) if the value of selectedOption is "other", the flag activateOtherInput is activated; otherwise, it is off.
3) the other input textfield appears depending on the flag activateOtherInput.
The disadvantage of this method is an additional round trip to the server, but you avoid writing your own javascript.
class ExampleForm{
private
String selectedOption;
private String otherInput;
private boolean activateOtherInput;
public void selectedOptionChanged(ValueChangeEvent event){
if(selectedOption.equals("other") //Or whatever value you might have
activateOtherInput = true;
else
activateOtherInput = false;
FacesContext context = FacesContext.getCurrentInstance();
selectedOption = (String)event.getNewValue();
context.renderResponse();
}
...
//Rest of code
}
example.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ page contentType="text/html;charset=windows-1252"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<html><head></head><body>
<h:selectOneMenu value="#{exampleForm.selectedOption}" onchange="submit();"
valueChangeListener="#{exampleForm.selectedOptionChanged}">
<f:selectItem value="PC" label="PC"/>
<f:selectItem value="Mac" label="Mac"/>
<f:selectItem value="other" label="Other"/>
</h:selectOneMenu>
<h:inputText value="#{exampleForm.otherInput}" rendered="#{exampleForm.activateOtherInput}"></f:inputText>
</body>
</html>