Copy data from one field to another

Asked

Viewed 1,051 times

2

My client, requested that I create 2 fields in the system, one as "Cellular" other as "Whatsapp" and that next to the Whatsapp field had a button to copy the value, if the number is the same, someone has idea how I can do?

  • It’s not very clear what you want. Could you be more specific? Showing the code you have today can be helpful

  • @Randrade are two phone fields, one for mobile, another for Whatsapp, but some people have the same number for both, and then, he wants to copy the value of the phone to paste in Whatsapp, but would be like this, click the button, it already picks the value that is on mobile, and passes to the field Whatsapp

  • I haven’t done anything yet... I just got the screen (CRUD) generated by scaffold

2 answers

6


Without much detail it is difficult to help you, but using jQuery, the basis is this

$('#bt-copiar').on('click', function(){
  $('#txt-wpp').val($('#txt-tel').val());    
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>Telefone:</label>
<input type="text" id="txt-tel" />
<button id="bt-copiar">Copiar</button>

<br>

<label>Whatsapp</label>
<input type="text" id="txt-wpp" />

5

Only with Javascript can you do something like this:

function copiaValor() {
  var celular = document.getElementById("campo1").value;
  document.getElementById("campo2").value = celular;
}
<label>Telefone:</label>
<input type="text" id="campo1" />

<br>

<label>Whatsapp</label>
<input type="text" id="campo2" />

<button id="copiar" onclick="copiaValor()">Copiar</button>

Browser other questions tagged

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