How can we get the selected value of PrimeFaces <p:selectOneMenu>
using JavaScript/jQuery?
I am trying to get it this way, but it does not go inside if condition, which means that the ID of the element is not correct.
<h:head>
<script>
function showDialog() {
alert("insdie function");
if($('#someSelect').val() == 'India') {
dlg.show();
alert("after function");
}
alert("outside function");
}
</script>
</h:head>
<h:body>
<h:form>
<p:panel>
<h:panelGrid columns="2">
<p:selectOneMenu
id="someSelect"
value="#{testController.countryName}"
<f:selectItem itemLabel="Select One" itemValue="" />
<f:selectItems value="#{addPatientProfileBB.patStatusSelect}"
itemLabel="#{testController.countryName}"
itemValue="#{testController.countryNameId}" />
<p:ajax process="someSelect" update="dialog" oncomplete="showDialog()"/>
</p:selectOneMenu>
</h:panelGrid>
<p:dialog id="dialog" header="Login" widgetVar="dlg">
<h:form>
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel for="username" value="Username:" />
<p:inputText id="username" required="true" label="username" />
</h:panelGrid>
</h:form>
</p:dialog>
</p:panel>
</h:form>
</h:body>
try changing
if($('#someSelect').val() == 'India') {
into
if($("select[name$='someSelect_input'] option:selected").val() == 'India') {
EDIT
you can improve the selector by changing
name$='someSelect_input'
into
name='yourFormName\\:someSelect_input'