JQuery datepicker callback functions and blur event










1















I have a trivial age calculator which calculates the age of a user upon sign up. If the age was 18 or more, the text in the result field is green, if the age was less than 18, the text is red.



Everything works, except I want the result field to be animated into view once the calculation is done. The result element should be out of view until both dates are picked.



I do not have a sumbit button, because I am not sure how to make that work with the datepicker instances. Also, I do not know when the blur events are fired on the datepicker instances, everything runs "under the hood".



The relevant HTML (you will see there is the jQuery approach and the HTML input type date approch, you can ignore that)



<div class="jq">
<h1>With jQuery</h1>
<span> enter DOB</span>
<input type="text" id="date1"> // get sign date of birth

<span> enter sign up date</span>
<input type="text" id="date2"> // get sign up date
<span> Result</span>
<input type="text" id="calculated" class="outOfView"> // display result (date2.value - date1.value)
</div>


The relevant CSS:



.red 
color: red;
transform: scale(5) rotate(360deg);

.green
color: green;



.outOfView
transform: translateY(50px); // this is just a test value


.inView
transform: translateY(0);



This is straight forward, I want the id="calculated" input field to be out of view initially and whenever initializing a new calculation( here I do not know whether to choose a method of the datepicker object or my own function on a blur or focusout event).



And once the date difference is calculated, or, the id="calculated" input field is not empty, I want to animate that element into view, by adding the class .inView.



Here is the JS:



$(function() 
$("#date1").datepicker( // initializing first datepicker
showAnim: "clip",
changeMonth: true,

changeYear: true,

yearRange: "1930:2010"
);

$("#date1").datepicker(
onSelect: calcDiff, // onselect is a datepicker method, calcDiff is my own function

minDate: null,
maxDate: null
);

$("#date2").datepicker( // initializing/configuring the second datepicker input field
changeMonth: true,

changeYear: true,

yearRange: "1930:2018",
showAnim: "slide",
onSelect: calcDiff,
onClose: move, // onclose is a method of the datepicker object, move is my function where I want to toggle the inView / outOfView classes

minDate: null,
maxDate: null
);

function calcDiff() // this is just the math, this works
var d1 = $("#date1").datepicker("getDate");

var d2 = $("#date2").datepicker("getDate");

var diff = 0;

if (d1 && d2)
diff = Math.floor((d2.getTime() - d1.getTime()) / 86400000); // ms per day


var years = Math.floor(diff / 365);

var days = Math.floor(diff % 365);

var months = Math.floor(days % 12);
// here things get a bit complicated, but this still works
if (years < 18)
$("#calculated").removeClass("green");

$("#calculated").addClass("red");
else
$("#calculated").removeClass("red");

$("#calculated").addClass("green");


$("#calculated").val("age was " + years + "y, " + " and " + days + "d");

// here is the problem, this does not work
function move()
if ($("#calculated").val !=="") // this if clause is likely the wrong approach
$("#calculated").removeClass("outOfView");
$("#calculated").addClass("inView");

else
$("#calculated").removeClass("inView");
$("#calculated").addClass("outOfView");



);


All this works on first try or if I refresh the page.



But I want the element out of view, by adding the class .outOfView whenever a new date is picked on either of the datepicker input fields.



Or, I want the value of the id="calculated" to be =="" as soon a new date is picked on either datepickers.



How can I achieve this, I hope my intent and the problem are clear.



Link to codepen:



https://codepen.io/damPop/pen/oayEdg?editors=1010










share|improve this question


























    1















    I have a trivial age calculator which calculates the age of a user upon sign up. If the age was 18 or more, the text in the result field is green, if the age was less than 18, the text is red.



    Everything works, except I want the result field to be animated into view once the calculation is done. The result element should be out of view until both dates are picked.



    I do not have a sumbit button, because I am not sure how to make that work with the datepicker instances. Also, I do not know when the blur events are fired on the datepicker instances, everything runs "under the hood".



    The relevant HTML (you will see there is the jQuery approach and the HTML input type date approch, you can ignore that)



    <div class="jq">
    <h1>With jQuery</h1>
    <span> enter DOB</span>
    <input type="text" id="date1"> // get sign date of birth

    <span> enter sign up date</span>
    <input type="text" id="date2"> // get sign up date
    <span> Result</span>
    <input type="text" id="calculated" class="outOfView"> // display result (date2.value - date1.value)
    </div>


    The relevant CSS:



    .red 
    color: red;
    transform: scale(5) rotate(360deg);

    .green
    color: green;



    .outOfView
    transform: translateY(50px); // this is just a test value


    .inView
    transform: translateY(0);



    This is straight forward, I want the id="calculated" input field to be out of view initially and whenever initializing a new calculation( here I do not know whether to choose a method of the datepicker object or my own function on a blur or focusout event).



    And once the date difference is calculated, or, the id="calculated" input field is not empty, I want to animate that element into view, by adding the class .inView.



    Here is the JS:



    $(function() 
    $("#date1").datepicker( // initializing first datepicker
    showAnim: "clip",
    changeMonth: true,

    changeYear: true,

    yearRange: "1930:2010"
    );

    $("#date1").datepicker(
    onSelect: calcDiff, // onselect is a datepicker method, calcDiff is my own function

    minDate: null,
    maxDate: null
    );

    $("#date2").datepicker( // initializing/configuring the second datepicker input field
    changeMonth: true,

    changeYear: true,

    yearRange: "1930:2018",
    showAnim: "slide",
    onSelect: calcDiff,
    onClose: move, // onclose is a method of the datepicker object, move is my function where I want to toggle the inView / outOfView classes

    minDate: null,
    maxDate: null
    );

    function calcDiff() // this is just the math, this works
    var d1 = $("#date1").datepicker("getDate");

    var d2 = $("#date2").datepicker("getDate");

    var diff = 0;

    if (d1 && d2)
    diff = Math.floor((d2.getTime() - d1.getTime()) / 86400000); // ms per day


    var years = Math.floor(diff / 365);

    var days = Math.floor(diff % 365);

    var months = Math.floor(days % 12);
    // here things get a bit complicated, but this still works
    if (years < 18)
    $("#calculated").removeClass("green");

    $("#calculated").addClass("red");
    else
    $("#calculated").removeClass("red");

    $("#calculated").addClass("green");


    $("#calculated").val("age was " + years + "y, " + " and " + days + "d");

    // here is the problem, this does not work
    function move()
    if ($("#calculated").val !=="") // this if clause is likely the wrong approach
    $("#calculated").removeClass("outOfView");
    $("#calculated").addClass("inView");

    else
    $("#calculated").removeClass("inView");
    $("#calculated").addClass("outOfView");



    );


    All this works on first try or if I refresh the page.



    But I want the element out of view, by adding the class .outOfView whenever a new date is picked on either of the datepicker input fields.



    Or, I want the value of the id="calculated" to be =="" as soon a new date is picked on either datepickers.



    How can I achieve this, I hope my intent and the problem are clear.



    Link to codepen:



    https://codepen.io/damPop/pen/oayEdg?editors=1010










    share|improve this question
























      1












      1








      1








      I have a trivial age calculator which calculates the age of a user upon sign up. If the age was 18 or more, the text in the result field is green, if the age was less than 18, the text is red.



      Everything works, except I want the result field to be animated into view once the calculation is done. The result element should be out of view until both dates are picked.



      I do not have a sumbit button, because I am not sure how to make that work with the datepicker instances. Also, I do not know when the blur events are fired on the datepicker instances, everything runs "under the hood".



      The relevant HTML (you will see there is the jQuery approach and the HTML input type date approch, you can ignore that)



      <div class="jq">
      <h1>With jQuery</h1>
      <span> enter DOB</span>
      <input type="text" id="date1"> // get sign date of birth

      <span> enter sign up date</span>
      <input type="text" id="date2"> // get sign up date
      <span> Result</span>
      <input type="text" id="calculated" class="outOfView"> // display result (date2.value - date1.value)
      </div>


      The relevant CSS:



      .red 
      color: red;
      transform: scale(5) rotate(360deg);

      .green
      color: green;



      .outOfView
      transform: translateY(50px); // this is just a test value


      .inView
      transform: translateY(0);



      This is straight forward, I want the id="calculated" input field to be out of view initially and whenever initializing a new calculation( here I do not know whether to choose a method of the datepicker object or my own function on a blur or focusout event).



      And once the date difference is calculated, or, the id="calculated" input field is not empty, I want to animate that element into view, by adding the class .inView.



      Here is the JS:



      $(function() 
      $("#date1").datepicker( // initializing first datepicker
      showAnim: "clip",
      changeMonth: true,

      changeYear: true,

      yearRange: "1930:2010"
      );

      $("#date1").datepicker(
      onSelect: calcDiff, // onselect is a datepicker method, calcDiff is my own function

      minDate: null,
      maxDate: null
      );

      $("#date2").datepicker( // initializing/configuring the second datepicker input field
      changeMonth: true,

      changeYear: true,

      yearRange: "1930:2018",
      showAnim: "slide",
      onSelect: calcDiff,
      onClose: move, // onclose is a method of the datepicker object, move is my function where I want to toggle the inView / outOfView classes

      minDate: null,
      maxDate: null
      );

      function calcDiff() // this is just the math, this works
      var d1 = $("#date1").datepicker("getDate");

      var d2 = $("#date2").datepicker("getDate");

      var diff = 0;

      if (d1 && d2)
      diff = Math.floor((d2.getTime() - d1.getTime()) / 86400000); // ms per day


      var years = Math.floor(diff / 365);

      var days = Math.floor(diff % 365);

      var months = Math.floor(days % 12);
      // here things get a bit complicated, but this still works
      if (years < 18)
      $("#calculated").removeClass("green");

      $("#calculated").addClass("red");
      else
      $("#calculated").removeClass("red");

      $("#calculated").addClass("green");


      $("#calculated").val("age was " + years + "y, " + " and " + days + "d");

      // here is the problem, this does not work
      function move()
      if ($("#calculated").val !=="") // this if clause is likely the wrong approach
      $("#calculated").removeClass("outOfView");
      $("#calculated").addClass("inView");

      else
      $("#calculated").removeClass("inView");
      $("#calculated").addClass("outOfView");



      );


      All this works on first try or if I refresh the page.



      But I want the element out of view, by adding the class .outOfView whenever a new date is picked on either of the datepicker input fields.



      Or, I want the value of the id="calculated" to be =="" as soon a new date is picked on either datepickers.



      How can I achieve this, I hope my intent and the problem are clear.



      Link to codepen:



      https://codepen.io/damPop/pen/oayEdg?editors=1010










      share|improve this question














      I have a trivial age calculator which calculates the age of a user upon sign up. If the age was 18 or more, the text in the result field is green, if the age was less than 18, the text is red.



      Everything works, except I want the result field to be animated into view once the calculation is done. The result element should be out of view until both dates are picked.



      I do not have a sumbit button, because I am not sure how to make that work with the datepicker instances. Also, I do not know when the blur events are fired on the datepicker instances, everything runs "under the hood".



      The relevant HTML (you will see there is the jQuery approach and the HTML input type date approch, you can ignore that)



      <div class="jq">
      <h1>With jQuery</h1>
      <span> enter DOB</span>
      <input type="text" id="date1"> // get sign date of birth

      <span> enter sign up date</span>
      <input type="text" id="date2"> // get sign up date
      <span> Result</span>
      <input type="text" id="calculated" class="outOfView"> // display result (date2.value - date1.value)
      </div>


      The relevant CSS:



      .red 
      color: red;
      transform: scale(5) rotate(360deg);

      .green
      color: green;



      .outOfView
      transform: translateY(50px); // this is just a test value


      .inView
      transform: translateY(0);



      This is straight forward, I want the id="calculated" input field to be out of view initially and whenever initializing a new calculation( here I do not know whether to choose a method of the datepicker object or my own function on a blur or focusout event).



      And once the date difference is calculated, or, the id="calculated" input field is not empty, I want to animate that element into view, by adding the class .inView.



      Here is the JS:



      $(function() 
      $("#date1").datepicker( // initializing first datepicker
      showAnim: "clip",
      changeMonth: true,

      changeYear: true,

      yearRange: "1930:2010"
      );

      $("#date1").datepicker(
      onSelect: calcDiff, // onselect is a datepicker method, calcDiff is my own function

      minDate: null,
      maxDate: null
      );

      $("#date2").datepicker( // initializing/configuring the second datepicker input field
      changeMonth: true,

      changeYear: true,

      yearRange: "1930:2018",
      showAnim: "slide",
      onSelect: calcDiff,
      onClose: move, // onclose is a method of the datepicker object, move is my function where I want to toggle the inView / outOfView classes

      minDate: null,
      maxDate: null
      );

      function calcDiff() // this is just the math, this works
      var d1 = $("#date1").datepicker("getDate");

      var d2 = $("#date2").datepicker("getDate");

      var diff = 0;

      if (d1 && d2)
      diff = Math.floor((d2.getTime() - d1.getTime()) / 86400000); // ms per day


      var years = Math.floor(diff / 365);

      var days = Math.floor(diff % 365);

      var months = Math.floor(days % 12);
      // here things get a bit complicated, but this still works
      if (years < 18)
      $("#calculated").removeClass("green");

      $("#calculated").addClass("red");
      else
      $("#calculated").removeClass("red");

      $("#calculated").addClass("green");


      $("#calculated").val("age was " + years + "y, " + " and " + days + "d");

      // here is the problem, this does not work
      function move()
      if ($("#calculated").val !=="") // this if clause is likely the wrong approach
      $("#calculated").removeClass("outOfView");
      $("#calculated").addClass("inView");

      else
      $("#calculated").removeClass("inView");
      $("#calculated").addClass("outOfView");



      );


      All this works on first try or if I refresh the page.



      But I want the element out of view, by adding the class .outOfView whenever a new date is picked on either of the datepicker input fields.



      Or, I want the value of the id="calculated" to be =="" as soon a new date is picked on either datepickers.



      How can I achieve this, I hope my intent and the problem are clear.



      Link to codepen:



      https://codepen.io/damPop/pen/oayEdg?editors=1010







      javascript jquery html jquery-ui-datepicker






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 20:12









      pttsptts

      581413




      581413






















          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327220%2fjquery-datepicker-callback-functions-and-blur-event%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327220%2fjquery-datepicker-callback-functions-and-blur-event%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Barbados

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3