JS How to auto-refill an input with Javascript?

Asked

Viewed 127 times

0

Hello

I need to make a button that when clicking it automatically fills the input fields.

HTML

<button type="button" id="botao">Auto</button>
<input type="text" id="nome">
<input type="text" id="sobrenome">

JS

$(document).ready(function() {
   var nome = "Joãozinho";
   var sobrenome = "Fulano";

   $("#botao").click(function(){
      $("#nome").val(nome);
      $("#sobrenome").val(sobrenome);
   });
});

But when clicking the button nothing occurs, the is wrong in the code?

  • 1

    The code works. Confirm that you have jquery correctly added to the page.

  • Really missed jquery, thank you so much for helping :)

1 answer

3


Code is correct, see working here

$(document).ready(function() {
   var nome = "Joãozinho";
   var sobrenome = "Fulano";

   $("#botao").click(function(){
      $("#nome").val(nome);
      $("#sobrenome").val(sobrenome);
   });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button type="button" id="botao">Auto</button>
<input type="text" id="nome">
<input type="text" id="sobrenome">

Browser other questions tagged

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