1

I am trying to configure on how to add a form whenever you click the add button. Can someone help me with this? I attached a jsfiddle so that you can fully understand what I am saying

<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

<div style="display: none;" id = "form1">
                  
  <form action="" method="" id = "add-order-form">
  <div class="row add-order-info text-center">
    <div class="col"> 
      <div class="dropdown">
        <button type="button" class="btn dropdown-toggle" 
        data-bs-toggle="<dropdown">
        Select...
        </button>
          <ul class="dropdown-menu">
            <li><a class="dropdown-item" href="#">Mouse</a></li>
            <li><a class="dropdown-item" href="#">Monitor</a></li> 
            <li><a class="dropdown-item" href="#">Keyboard</a></li>
          </ul>
        </div>
      </div>
   </div>
   </form>
</div>
<button type="button" value ="Add Child" onclick="addForm();" id = "add-button">
Add
</button>
                

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
 function addForm() {
        document.getElementById('form1').style.display = 'block';
        
        
        $("#add-order-form").clone().appendTo("#form1");
       
        }

https://jsfiddle.net/u0pzeqry/1/

1
  • You may want to read up on <template>. Commented Jan 24, 2023 at 2:20

2 Answers 2

2

We're going to create a clone of your form and add it to a container div. Here's the HTML:

function addForm() {
   var formsContainer = document.getElementById("formsContainer");
   var newForm = document.getElementById("form1").cloneNode(true);
   newForm.style.display = "block";
   formsContainer.appendChild(newForm);
 }
<div style="display: none;" id = "form1">

  <form action="" method="" id = "add-order-form">
  <div class="row add-order-info text-center">
    <div class="col"> 
      <div class="dropdown">
        <button type="button" class="btn dropdown-toggle" 
        data-bs-toggle="dropdown">
        Select...
        </button>
          <ul class="dropdown-menu">
            <li><a class="dropdown-item" href="#">Mouse</a></li>
            <li><a class="dropdown-item" href="#">Monitor</a></li> 
            <li><a class="dropdown-item" href="#">Keyboard</a></li>
          </ul>
        </div>
      </div>
   </div>
   </form>
</div>

 <div id="formsContainer"></div>

<button type="button" value ="Add Child" onclick="addForm();" id = "add-button">
Add
</button>

Here's a fork of your JS fiddle with the code working:

https://jsfiddle.net/TrostCodes/7cdhfqkn/8/

Sign up to request clarification or add additional context in comments.

1 Comment

There is a typo in the code: data-bs-toggle="<dropdown"> remove angular bracket it should be like this: data-bs-toggle="dropdown">
0

you can use js side instead of using HTML clone. I think that would be the ideal method for that.

var formcounter = 0;
function setForms(){  
  $("#wrapperFormContainer").append( '<form action="" method=""   id="add-order-form'+ formcounter +'"><div>add-order-form'+formcounter+'</div></form>');
  formcounter++;
}
#wrapperFormContainer{
  display:inline-block;
  width:100%;
  background-color:green;
}
#wrapperFormContainer form{
  display:inline-block;
  margin: 5px;
  background-color:red;
  border:solid 1px #fff;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="wrapperFormContainer">
  
</div>
<button type="button" onclick="setForms()">Add form</button>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.