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 8147871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:30:00+00:00 2026-06-06T14:30:00+00:00

I have a form to which I am appending form elements on the click

  • 0

I have a form to which I am appending form elements on the click event. I want to append options to a drop down(select options) which is created dynamically as the user types the input.

The user input is saved in a variable as he types using the keyup event in the following manner:

$("#options_"+id).keyup(function(){

        var val_options = $('#options_'+id).val(); //user input is stored in the variable val_options

        var options = val_options.split(','); //input is exploded with a comma       

        var len = options.length;

            for(var i =0;i<len;i++)
            {                   

                var drop_options = "<option id='option_'"+i+">"+options[i]+"</option>";
                $("#select_"+id).append(drop_options);                  
            }

    });

Now the tags created dynamically are appended to the tag having that particular id.

The problem I am facing here is that when the user types a single character, it gets appended as an option in the drop down menu. I only want it to append when the user presses the comma (,) key.

  • 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-06T14:30:02+00:00Added an answer on June 6, 2026 at 2:30 pm
    $("#options_" + id).keyup(function(e) {
    
        var val_options = $(this).val(); // getting input values
            options = val_options.split(','); // getting all values as array
    
        // checking for comma
        if (val_optons.indexOf(',') >= 0 ) {
    
            // if comma found then start append
            var options = val_options.split(',');
            var drop_options = ''; // take an empty string
    
            // looping over all values with
            // input fields separated by comma
    
            for (var i = 0; i < options.length; i++) {
    
                // making string of all options
                drop_options += "<option id='option_'" + i + ">" + options[i] + "</option>";
            }
    
            // append all option to select
            // here .html() will remove all previous
            // options and append newly 
    
            $("#select_" + id).html(drop_options);
    
        }
    });
    

    DEMO

    Calling .append() within loop cause slower performance, so try to avoid that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have dynamically created option elements with javascript. I double click an option from
I have a form which has elements in a couple of divs. Based on
Has anyone tried to dynamically select which properties they want to write to an
We have a table form which we would like to select data in both
i do use symfony2.1 and i have form which i do extend by another
I have a form which POST's its data via AJAX: $.ajax({ type: POST, url:
I have a form which has three inputs when the page loads -- 2
We have a form which uses the JQuery Validation Plugin and would like to
I have a form which consists of 2 steps. What I'd like to do
i have an form which will send some data to an php file ,

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.