Duplicate inputs with bootstrap

Asked

Viewed 456 times

2

I’m creating a form with tabs and I have to duplicate a set of inputs to another line and wanted help to do this. I’m using bootstrap and there’s the code snippet to be duplicated.

<form class="formulario" role="form">
  <div class="tab-content">
    
    <div class="tab-pane active" role="tabpanel" id="step1">
      <div class="row">
        <h4>
            <i class="fa fa-comments"></i>
            Encontros presenciais
        </h4>
        </div>

        <div class="row">
        <div class="col-md-4">
            <div class="form-group">
                <label for="local-encontpresen">Local:</label>
                <input type="text" class="form-control" name="local-encontpresen" id="local-encontpresen">
            </div>
        </div>

        <div class="col-md-3">
            <div class="form-group">
                <label for="data-encontpresen">Data:</label>
                <input type="date" class="form-control" name="data-encontpresen" id="data-encontpresen">
            </div>
        </div>

        <div class="col-md-3">
            <div class="form-group">
                <label for="tipo-avl-encontpresen">Tipo de Avaliação:</label>
                <select id="tipo-avl-encontpresen" class="form-control">
                    <option hidden="true">Selecione</option>
                    <option>Prova tradicional</option>
                    <option>Prova via Moodle</option>
                </select>
            </div>
        </div>
        
        <div class="col-md-1">
            <div class="form-group">
                <label for="peso-encontpresen">Peso:</label>
                <input type="number" class="form-control" name="peso-encontpresen" id="peso-encontpresen" step="1" min="0">
            </div>
        </div>

        <div class="col-md-1">
             <div class="form-group">
                 <button class="btn-adicionar">Adicionar</button>
                 <button class="btn-adicionar">Adicionar</button>
             </div>
        </div>
      </div>
    </div>
    
    <div class="row">
      <ul class="list-inline pull-right">
        <li><button type="button" class="btn btn-primary next-step">Salvar e continuar</button></li>
      </ul>
    </div>
    
  </div>
</form>

Botões De adicionar e remover no final da **row**

In this second image I have the image of the tabs created:

Segunda imagem das abas

Project link on Github

  • Pq has 2 buttons "Add"?

  • I forgot to edit... It was to simulate an add and another remove..

1 answer

2


How you plan to create multiple elements with the same name, should convert them to array by adding [] at the name:

Ex.: name="peso-encontpresen[]"

To duplicate you can create a variable with the div containing the particulars on the form:

var clone_step1 = $("#step1").find(".row:eq(1)").html(); // cópia da div com os inputs
$(document).on("click", ".btn-adicionar", function(e){
   $("#step1")
   .find(".row:eq(1)")
   .append(clone_step1); // insere nova linha

   $(this)
   .closest(".col-md-1")
   .remove(); // remove o botão adicionar
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">

<form class="formulario" role="form">
   <div class="tab-content">
      <div class="tab-pane active" role="tabpanel" id="step1">
         <div class="row">
            <h4>
               <i class="fa fa-comments"></i>
               Encontros presenciais
            </h4>
         </div>
         <div class="row">
            <div class="col-md-4">
               <div class="form-group">
                  <label for="local-encontpresen">Local:</label>
                  <input type="text" class="form-control" name="local-encontpresen[]" id="local-encontpresen">
               </div>
            </div>
            <div class="col-md-3">
               <div class="form-group">
                  <label for="data-encontpresen">Data:</label>
                  <input type="date" class="form-control" name="data-encontpresen[]" id="data-encontpresen">
               </div>
            </div>
            <div class="col-md-3">
               <div class="form-group">
                  <label for="tipo-avl-encontpresen">Tipo de Avaliação:</label>
                  <select id="tipo-avl-encontpresen" class="form-control">
                     <option hidden="true">Selecione</option>
                     <option>Prova tradicional</option>
                     <option>Prova via Moodle</option>
                  </select>
               </div>
            </div>
            <div class="col-md-1">
               <div class="form-group">
                  <label for="peso-encontpresen">Peso:</label>
                  <input type="number" class="form-control" name="peso-encontpresen[]" id="peso-encontpresen" step="1" min="0">
               </div>
            </div>
            <div class="col-md-1">
               <div class="form-group">
                  <button class="btn-adicionar">Adicionar</button>
               </div>
            </div>
         </div>
      </div>
      <div class="row">
         <ul class="list-inline pull-right">
            <li><button type="button" class="btn btn-primary next-step">Salvar e continuar</button></li>
         </ul>
      </div>
   </div>
</form>

  • Like I said, I don’t know if it’s because it’s inside a tab, but when I do this you suggested to me, it just hides the add button and doesn’t duplicate any lines.

  • So it has to see the code straight. Now I can’t because I’m on the cell phone, but tomorrow I see it right blz?

  • See if you are calling AFTER html has been loaded. It may be that this is not happening and the code does not find the html and variable var clone_step1 = $("#step1").find(".row:eq(1)").html(); is empty.

  • I’m putting in a . js and calling him inside the head.

  • So that’s it... put it inside a $(document).ready(function(){ código aqui });

  • He’s doubling now, but the top line, not the line of the inputes being duplicated

  • Exchange the eq(1) for eq(2) to see if it works.

  • As it has several Rows, the number no eq() will take the Row you want, the first Row is zero, the second 1 and so on

  • Dude, you better put a specific class on the Row that you want to duplicate instead of staying depending on picking up the index with :eq()... places a class in the div along with Row: class="row tab1" and you take the inputs like this: $("#step1").find(".tab1").html();

  • Well, it adds this way, but goes back to the top of the page and after adding some fields it restarts.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.