JS How to auto-fill an input with JavaScript?

1

Hello

I need to make a button that when it clicks it automatically fills in 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 you click the button nothing happens, what is wrong with the code?

    
asked by anonymous 04.08.2017 / 02:56

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><buttontype="button" id="botao">Auto</button>
<input type="text" id="nome">
<input type="text" id="sobrenome">
    
04.08.2017 / 03:17