Jump to content
Fórum Script Brasil
  • 0

Preencher automaticamente o input


dr.x

Question

Ao carregar a página, preciso preencher automanticamente
o input com id="codigo" pegando o valor ou texto da 
variável "res". Porém não está pegando.

<html>
<head>
<script src="assets/js/moment.min.js" type="text/javascript"></script>
<script type="text/javascript">
const agora = moment();
var ano = (agora.format("YYYY"));
var dia = (agora.format("DD"));
var mes = (agora.format("MM"));
var res = (ano + "/" + "sigla" + dia + mes + "_01")
$('#codigo').val(res);
</script>
</head>
<body>
<input type="text" id="codigo">
</body>
</html>

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Quando o seu javascript foi executado o input ainda não existia na DOM
tente asssim:
 

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Autofill</title>
</head>

<body>

  <input type="text" id="codigo">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.30.1/moment.min.js"></script>
  <script type="text/javascript">
    const agora = moment();
    var ano = (agora.format("YYYY"));
    var dia = (agora.format("DD"));
    var mes = (agora.format("MM"));
    var res = (ano + "/" + "sigla" + dia + mes + "_01")
    $('#codigo').val(res);
  </script>
</body>

</html>

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.1k
    • Total Posts
      651.7k
×
×
  • Create New...