6

How I can get select component value using Jquery or javascript

MyJSF code below

<h:form>
      <p:selectOneMenu style="width:150px" id="id" onchange="onCall()">
              <f:selectItem itemLabel="Select" itemValue=""></f:selectItem>
              <f:selectItem itemLabel="Other" itemValue="Other"></f:selectItem>
       </p:selectOneMenu>
       <p:selectBooleanCheckbox id="flag"/>
       <p:inputText id="name" value="#{mybean.value}/>
 </h:form>

Please help me to solve this issue

Kuba
  • 2,069
  • 23
  • 30
JSF Learner
  • 183
  • 2
  • 7
  • 15
  • I have tried some code but no one work – JSF Learner Jan 23 '14 at 14:00
  • it would be better if you could post your rendered html, instead of jsf code this way, also if you could post your javascript as you mentioned you have applied some inline js event. – Jai Jan 23 '14 at 14:03

2 Answers2

14

As Primefaces provides a Javascript API for its components, you can access it through the widgetVar

xhtml

<p:selectOneMenu widgetVar="selectWV">               
</p:selectOneMenu>

JS

PF('selectWV').getSelectedValue();//gets the value
PF('selectWV').getSelectedLabel();//gets the label
Hatem Alimam
  • 9,968
  • 4
  • 44
  • 56
0

you can pass the this in this function:

onchange="onCall(this)"

and i am assuming you have defined this onCall function in the global scope, then you can do this:

onCall(el){
    alert(el.value);
}
Jai
  • 74,255
  • 12
  • 74
  • 103
  • why would you debug with `alert` and not `console.log()`? – Kuba Jan 23 '14 at 14:03
  • this is just an example where OP has to look for answers. I know `console.log()` is better to use but this is just i would say my assumption to tell OP try doing this way to get the values in your function. – Jai Jan 23 '14 at 14:05
  • Oh working, but i want set checkbox=true value as well – JSF Learner Jan 23 '14 at 14:05