Take the name and date value (using datepicker), and then calculate using only (JQUERY),

Asked

Viewed 1,745 times

4

 $(function () {
                
                $("#datepicker").datepicker({
                   
                });
                
            });
    <body>
        <form id="formu">
            Nome:
            <input type="text" id="name"><br>
            Data de nascimento:          
            <input type="text" id="datepicker" name="data"/><br>
            <input type="button" id="botton" value="Ok">            
        </form>
        <div id="resultado"></div>
    </body>

** user age. Next show in result div.**

1 answer

4


var esteAno = new Date().getFullYear();
$('#datepicker').datepicker({
  dateFormat: 'dd-mm-yy',
  onSelect: function(date) {
    var ano = date.split('-')[2]; // ano aqui
    var anos = esteAno - ano;
    $('#resultado').html('');
    var strAnos = 'anos';
    if(anos === 1) {
        strAnos = 'ano';
    }
    $('#resultado').html($('#name').val() +' você tem ' +anos+ ' ' +strAnos);
  }
});
<link href="https://code.jquery.com/ui/1.11.4/themes/pepper-grinder/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>


<form id="formu">
        Nome:
        <input type="text" id="name"><br>
        Data de nascimento:          
        <input type="text" id="datepicker" name="data"/><br>
        <input type="button" id="botton" value="Ok">            
    </form>
<div id="resultado"></div>

  • Only one question onSelect: function(date) { is to perform this function from the moment the user selects the date this?

  • Yap, from the moment you click/press a date @user49744

  • What does it mean in this part of the code: date.split('-')[2]; ?

  • We divide the date by the '-' and fetch the last of the set (array) @user49744.

  • And why you put $('#resultado').html(''); ?

  • To insert into the div #resultado

Show 1 more comment

Browser other questions tagged

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