I need to dynamically add values from a text input to a listbox field. Basically, I am making a custom multi-line text component. The following works with a standard <h:selectOneListbox>
, but when I use the PrimeFaces equivalent, <p:selectOneListbox>
, nothing seems to happen. I am using all PrimeFaces inputs in my application, and I want to keep the look consistent.
<!--This works:-->
<h:form id="mainForm">
<p:inputText id="txtInput" /><p:commandButton value="Add" id="addTxt"
onclick="addText()"/>
<h:selectOneListbox id="selectTxt"/>
<script type="text/javascript">
function addText(){
var txtInput = $('#mainForm\\:txtInput').val();
var opts = $('#mainForm\\:selectTxt').prop('options');
opts[opts.length] = new Option(txtInput,txtInput,true,true);
}
</script>
</h:form>
<!--This doesn't work:-->
<h:form id="mainForm">
<p:inputText id="txtInput" /><p:commandButton value="Add" id="addTxt"
onclick="addText()"/>
<p:selectOneListbox id="selectTxt"/>
<script type="text/javascript">
function addText(){
var txtInput = $('#mainForm\\:txtInput').val();
var opts = $('#mainForm\\:selectTxt_input').prop('options');
opts[opts.length] = new Option(txtInput,txtInput,true,true);
}
</script>
</h:form>
Thanks in advance for the help.