Copy data from one form to another

-1

I have 3 forms of 3 different employees, the forms are identical, sometimes they carry the same data in all three, is there any way that with javascript I can copy in the other two forms the information of the first to avoid filling the 3?

    
asked by Fernando Garcia 25.07.2018 в 19:12
source

3 answers

2

With Jquery, you could do a function that copies data from two forms with equal structures.

function cloneFormularios( $frm1 , $frm2 ) {
        $(':input[name]', $frm2).val(function() {
          return $(':input[name=' + this.name + ']', $frm1).val();
        });
      }
    //Al hacer click en un btn copiar
      $('#copiar').on('click', function() {
        cloneFormularios(  $('#f1') , $('#f2')  );
      });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<button id="copiar">copiar</button>
    
    <form id="f1">
     <input name="nombre" type="text">
      <input name="btninfo" type=button value="Info">
      <select name="cbTypo">
        <option value="1">a</option>
        <option value="2">b</option>
        <option value="3">c</option>
      </select>
    </form>
    
    <form id="f2">
       <input name="nombre" type="text">
       <input name="btninfo" type=button value="Info">
       <select name="cbTypo">
        <option value="1">a</option>
        <option value="2">b</option>
        <option value="3">c</option>
      </select>
    </form>
    
answered by 25.07.2018 / 19:20
source
-1

Once you do something similar doing this

function layout()
{
var layout2 = document.getElementById('layout');
layout2.innerHTML= 

// Acá iría todo el formulario 
;}

Once this is done, you call this function in your view in this way

<div id="layout">
  <script type="text/javascript">
    layout();
  </script>

</div>

and with that should appear the form you created

    
answered by 25.07.2018 в 19:23
-1

You can get the value of the fields in the first form and assign them to the new one with some action as a button. I'll give you an example:

Here I have 3 inputs of type text and a button.

<div class="container">
  <div class="row">
    <div class="col-4 offset-4">
      <input type="text" placeholder="Formulario 1" id="c1">
      <input type="text" placeholder="Formulario 2" id="c2">
      <input type="text" placeholder="Formulario 3" id="c3">
      <button id="boton" class="btn btn-block btn-primary">
        Copiar
      </button>
    </div>
  </div>
</div>

In the Javascript I get the value of the first input and assign it to the other two:

let btn = document.getElementById('boton');

btn.onclick = () => {
  let c1 = document.getElementById('c1').value;
  let c2 = document.getElementById('c2');
  let c3 = document.getElementById('c3');
  c2.value = c1;
  c3.value = c1;
}

You can see the example working in Codepen.

    
answered by 25.07.2018 в 19:28