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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:59:53+00:00 2026-06-08T06:59:53+00:00

I want to create a dynamic amount of fields determined by the value selected

  • 0

I want to create a dynamic amount of fields determined by the value selected in a dropDownList. As this will be executed client side it must be done through Javascript. I know I could achieve this effect by creating ALL of the fields before hand and then simply using css to show/hide each one, but this is a lot of repetitive code.

Through trial and error I’ve got it to kind of work, but it either deletes my values when changing my dropdownlist or adds too many boxes if I don’t have it delete the previously created boxes before creating new ones.

Is there a simple way to accomplish what I’m trying to do, without losing entered values on change of the dropdownlist? My attempt at making something is below.

function doAThing() {
    var attemptsValue = parseInt(document.forms['ProgramApplicationForm'].elements['VerificationPrimaryHomePhoneAttemptsDropDownList'].value);

    if (attemptsValue == null) {
        document.getElementById('VerificationPrimaryHomePhoneAttemptDate').innerHTML = null;
    } else if (document.getElementById('VerificationPrimaryHomePhoneAttemptDate').innerHTML == null) {
        for (counter = 1; counter < attemptsValue + 1; counter++) {
            document.getElementById('VerificationPrimaryHomePhoneAttemptDate').innerHTML += 
            '<label for="VerificationPrimaryHomePhoneAttemptDate' + counter + '">Attempt ' + counter + ' Date:</label><input type="text" name="VerificationPrimaryHomePhoneAttemptDate' +  counter + '" value="" id="p-dod-date-picker" maxlength="10" class="size-ten" placeholder="yyyy-mm-dd" title="yyyy-mm-dd" pattern="\d{4}-\d{2}-\d{2}"/>'; 
        }            
    } else {
        document.getElementById('VerificationPrimaryHomePhoneAttemptDate').innerHTML = null;

        for (counter = 1; counter < attemptsValue + 1; counter++) {
            document.getElementById('VerificationPrimaryHomePhoneAttemptDate').innerHTML += 
            '<label for="VerificationPrimaryHomePhoneAttemptDate' + counter + '">Attempt ' + counter + ' Date:</label><input type="text" name="VerificationPrimaryHomePhoneAttemptDate' +  counter + '" value="" id="p-dod-date-picker" maxlength="10" class="size-ten" placeholder="yyyy-mm-dd" title="yyyy-mm-dd" pattern="\d{4}-\d{2}-\d{2}"/>';             
        }
    }
}
  • 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-08T06:59:54+00:00Added an answer on June 8, 2026 at 6:59 am

    Use element.removeChild and element.appendChild instead of meddling with innerHTML. This will enable you to remove specific nodes in the DOM:

    <form name="ProgramApplicationForm">
    <select id="VerificationPrimaryHomePhoneAttemptsDropDownList" name="VerificationPrimaryHomePhoneAttemptsDropDownList">
        <option>0</option>
        <option>1</option>
        <option>2</option>
        <option>3</option>
    </select>
        <div id="VerificationPrimaryHomePhoneAttemptDate"></div>
    </form>
    
    function createLabelElement(i){
        /* Create the label */
        var label = document.createElement("label");
        label.appendChild(document.createTextNode("Attempt "+i+" Date:")); // add text
    
        /* create the input and set all attributes */
        var input = document.createElement("input");
        input.type = "text";
        input.value = "";
        input.id="p-dod-date-picker"; // ids should be unique!
        input.maxlength = 10;
        input.className = "size-ten";
        input.placeholder = "yyyy-mm-dd";
        input.title = "yyyy-mm-dd";
        input.pattern = "\d{4}-\d{2}-\d{2}";
    
         /* a label doesn't need "for", 
           you can actually put your input into the label element 
        */
        label.appendChild(input);
    
        return label; // return the created element
    }
    
    document.getElementById('VerificationPrimaryHomePhoneAttemptsDropDownList').addEventListener('change',function(){
       var numberOfElements = parseInt(this.value);
       var targetDiv = document.getElementById('VerificationPrimaryHomePhoneAttemptDate');   
    
       // As long we have to many elements remove them
       while(targetDiv.children.length > numberOfElements ){
            targetDiv.removeChild(targetDiv.lastChild);
       }
       // As long we have not enough elements add more
       while(targetDiv.children.length < numberOfElements){
            targetDiv.appendChild(createLabelElement(targetDiv.children.length + 1));
       }
    });
    

    Demo

    If you want you can still use innerHTML on the created labels, this will make the first function much smaller. Also note that null isn’t a number, it’s an object.

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

Sidebar

Related Questions

i want create Dynamic Slideshow in Jquery i'm Write this code var ctx =
I want to create a dynamic HTML5 canvas animation. This animation should use server
this table i want to create job_id dynamic Jobs Title text Job Description text
I have issue where i want to create Dynamic function which will do some
I want to create a dynamic menu that will get it's items from a
I want to create dynamic content based on this. I know it's somewhere, as
I want to create a dynamic DropDownList in ASP.NET, but after postback the DropDownList
I want to create dynamic tabPages in TabControl. In each tabPage I create dataGridView
I want to create dynamic number of thread which is depends on the database
Here is the problem details: 1) I want to create dynamic (ip based) download

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.