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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:56:56+00:00 2026-05-20T08:56:56+00:00

I am trying to limit the number of additional form input fields that a

  • 0

I am trying to limit the number of additional form input fields that a user can add dynamically to a file upload form to just 3. The form is loaded with one static input field and through javascript can add additional fields with an add button or remove additional form input fields with a remove button. Below is the html in it’s static form.

<fieldset>
        <legend>Upload your images</legend>
          <ol id="add_images">
            <li>
              <input type="file" class="input" name="files[]" />
            </li>
          </ol>
         <input type="button" name="addFile"  id="addFile" value="Add Another Image" onclick="window.addFile(this);"/>
</fieldset>

With javascript I would like to create a function where the number of child

  • elements are counted and if the number is equal to three then the “Add Another Image” button becomes disabled. In addition, if there are three

  • elements in the form the user – with the remove button – removes a child
  • then the “Add Another Image” button becomes enabled again.

    I think I’m may be missing some crucial lines of code. The below javascript code only allows me to add one additional input field before the Add Another Image button becomes disabled. Removing this field with the remove file button removes the field but the Add Another Image button is still disabled. Below is where I’m currently at with the javascript.

    function addFile(addFileButton) {
        var form = document.getElementById('add_images');
        var li = form.appendChild(document.createElement("li"));
    
        //add additional input fields should the user want to upload additional images.    
        var f = li.appendChild(document.createElement("input"));
        f.className="input";
        f.type="file";
        f.name="files[]";
    
        //add a remove field button should the user want to remove a file
        var rb = li.appendChild(document.createElement("input"));
        rb.type="button";
        rb.value="Remove File";
        rb.onclick = function () {
            form.removeChild(this.parentNode);
        }
        //create the option to dispable the addFileButton if the child nodes total "3"
        var nodelist;
        var count;
        nodelist = form.childNodes;
        count = nodelist.length;
    
        for(i = 0; i < count; i++) {
    
        if (nodelist[i] ==3) {
            document.getElementById("addFile").disabled = 'true';
        }    
        else { //if there are less than three keep the button enabled
            document.getElementById("addFile").disabled = 'false';  
        }
    }
    

    }

    • 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-05-20T08:56:56+00:00Added an answer on May 20, 2026 at 8:56 am

      Oh, OK, I’ve tested out the code now and see a couple of problems:

      1. You’re counting the number of child elements but this includes the text elements so there’s actually one for the <li> and one for the text within it.
      2. You’ve enclosed the true/false setting for the disabled property in quotes but it doesn’t work and always set’s it to false.
      3. The remove button doesn’t re-enable the add button.

      I found this to work:

          function addFile(addFileButton) {
              var form = document.getElementById('add_images');
              var li = form.appendChild(document.createElement("li"));
      
              //add additional input fields should the user want to upload additional images.
              var f = li.appendChild(document.createElement("input"));
              f.className="input";
              f.type="file";
              f.name="files[]";
      
              //add a remove field button should the user want to remove a file
              var rb = li.appendChild(document.createElement("input"));
              rb.type="button";
              rb.value="Remove File";
              rb.onclick = function () {
                  form.removeChild(this.parentNode);
                  toggleButton();
              }
              toggleButton();
          }
      
          function toggleButton() {
              var form = document.getElementById('add_images');
              //create the option to dispable the addFileButton if the child nodes total "3"
              var nodelist;
              var count;
              nodelist = form.childNodes;
              count = 0;
      
      
              for(i = 0; i < nodelist.length; i++) {
                  if(nodelist[i].nodeType == 1) {
                      count++;
                  }
              }
      
              if (count >= 3) {
                  document.getElementById("addFile").disabled = true;
              }
              else { //if there are less than three keep the button enabled
                  document.getElementById("addFile").disabled = false;
              }
          }
      
      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
        • Report

    Sidebar

    Related Questions

    Trying to make a make generic select control that I can dynamically add elements
    I am trying to limit the number of characters that appear in a title.
    I'm trying to limit the number of records from an XML feed that are
    Is there a limit to number of items that can be added to Flash
    I'm trying to limit the number of connections my server will accept using semaphores,
    I am trying to limit the number of elements returned with mislav's will paginate
    I'm trying to limit the number of objects in an array controller, but I
    I am trying to limit a user of my application to voting (liking in
    Based on This solution I am trying to develop a script that would limit
    In my PHP web application, I am trying to limit the number of CPU/memory

    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.