Search code examples
javascriptjsfjsf-2selectonelistbox

how to read text (value) from jsf selectonelistbox itemLabel text?


Below is my JSF

<h:selectOneMenu id="lstmonth"
required="true"
value="#{secdeal.month}">
<f:selectItem
    itemValue="JAN"
    itemLabel="01-January"/>
<f:selectItem
    itemValue="FEB"
    itemLabel="02-February"/>
<f:selectItem
    itemValue="MAR"
    itemLabel="03-March"/>

In javascript (In same file)

I want to read the selecteditem's itemLabel's text. (i.e: If I select 01-January, I want the same text into my var x)

x=getElementById('Form:lstmonth').itemLabel // not working x=getElementById('Form:lstmonth').innerText // not working -- returning all months

How to get the itemLabel text? please suggest

Actually I want x=01-January (selected item)


Solution

  • Here you go:

    var ele = document.getElementById("Form:lstmonth");
    var selectedIndex = ele.selectedIndex;
    var x = ele.options[selectedIndex].text;