How to get the value of & lt; p: selectOneMenu & gt; in jquery?

advertisements

Hi am trying to get the value of p:selectOneMenu from jquery , but i dint got as yet. am using JSF and primefaces as my UI component.

    <p:selectOneMenu style="width:150px" id="skill"
                value="#{loginBean.skill}" required="true" immediate="true"
                requiredMessage="Select your skill" label="skill" styleClass="someClassName">
                <f:selectItem itemLabel="Select" itemValue=""></f:selectItem>
                <f:selectItem itemLabel="Other" itemValue="Other"></f:selectItem>
                <f:selectItems value="#{loginBean.skillList}" var="item"
                    itemLabel="#{item}" itemValue="#{item}"></f:selectItems>
            </p:selectOneMenu>

In Html

<select id="skill_input" name="skill_input">
<option value="">Select</option>
<option value="Other">Other</option>
<option value="Tailoring">Tailoring</option>
<option value="Swimming">Swimming</option>
<option value="Roaming">Roaming</option>
</select>

thats my select one menu, my js is..

  var $element = $('.someClassName');

alert($element);

in the alert box i got as, [object Object], but not the selected value.

then i tried this,

   var $element = $('.someClassName').val();

alert($element);

but now i got an empty alert box.

then i tried this

 var $element = $("select[name='skill_input']:selected").val();

alert($element);

my alert box says undefined

What else i should do to get the selected value in that alert box..??


Try this

var $element = $("select[name='skill_input'] option:selected").val();

alert($element);