Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8600377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:36:12+00:00 2026-06-12T01:36:12+00:00

I need to dynamically add values from a text input to a listbox field.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T01:36:14+00:00Added an answer on June 12, 2026 at 1:36 am

    The <p:selectOneListbox> uses an <ul><li> instead of <select><option> to present the options (with the simple reason that there’s then so much more freedom to give it a different look’n’feel). That’s exactly the reason why you can’t just create and add new Option elements. Rightclick page in browser and do View Source and you’ll understand it much better (if you’re already familiar with basic HTML, of course).

    Please note that adding new options this way will indeed seem to work for <h:selectOneListbox>, but that this is purely visually, in the client side. JSF won’t accept the in the client side created items on submit, but throw a Validation Error: Value not valid with the simple reason that the submitted value is not part of the items as available in the server-side definied <f:selectItems>.

    You need to use JSF instead of JavaScript to manipulate the <f:selectItems> value. This way it will work in <p:selectOneListbox> as well.

    Here’s a concrete kickoff example how to do it using pure JSF:

    <h:form>
        <p:inputText value="#{bean.text}" />
        <p:commandButton value="Add" action="#{bean.add}" update="listbox" />
        <p:selectOneListbox id="listbox">
            <f:selectItems value="#{bean.items}" />
        </p:selectOneListbox>
    </h:form>
    

    With:

    @ManagedBean
    @ViewScoped
    public class Bean {
    
        private String text; // +getter+setter
        private List<String> items; // +getter
    
        @PostConstruct
        public void init() {
            items = new ArrayList<String>();
        }
    
        public void add() {
            items.add(text);
        }
    
        // ...
    }
    

    That’s all.

    See also:

    • What is the need of JSF, when UI can be achieved from CSS, HTML, JavaScript, jQuery?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using a jQuery plugin to dynamically add input text fields. The name of
This is my script I use to dynamically add/remove input text fields. I'm using
I have the following table dynamically generated from a view. I need to add
I have a GridView that I need to dynamically add TemplateField elements to. My
I have a table of data that I need to dynamically add a column
I have a dataGrid(not dataGridView) in which i need to add Checkbox dynamically at
I need to add css height to each page dynamically with jQuery. So far
I need to be able to add rows to a table dynamically with jQuery
I need to create several Divs dynamically, and I need to add onmouseover event
I have a DataGrid and I need to add an Expander control dynamically to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.