Pass value from a function to value in Javascript input

Asked

Viewed 71 times

0

I have an input where the person should enter the date of birth, when she type, automatically already calls a function that makes the calculation of age. After that I would like the age value to be automatically passed to input value below birth date, but I’m not getting it.

//input recebe a data de nascimento
<input class="form-control fc-datepicker" placeholder="DD/MM/AAAA" type="text" id="data_nascimento" onblur="calcularIdade(this.value);">

//calcula a idade  
    <script type = "text/javascript" >
      function calcularIdade(aniversario) {
        var nascimento = aniversario.split("/");
        var dataNascimento = new Date(parseInt(nascimento[2], 10),
          parseInt(nascimento[1], 10) - 1,
          parseInt(nascimento[0], 10));

        var diferenca = Date.now() - dataNascimento.getTime();
        var idade = new Date(diferenca);

        return alert(Math.abs(idade.getUTCFullYear() - 1970));

    </script>

//input que recebe a idade
    <input class="form-control fc-datepicker" value="" disabled type="text" id="idade">

1 answer

2


Just take input by id and change its value:

document.getElementById("idade").value = Math.abs(idade.getUTCFullYear() - 1970);

Only you have to do it before the return and after the variable idade.

Browser other questions tagged

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