Grady D
Grady D

Reputation: 2019

Function never calls the second function

I have this javascript code

function editRerenderFix() {
  console.log("edit render start");
  textAreaFix();
  console.log("edit render middle");
  setupDates();
  console.log("edit render end");
}
/** Function to auto expand out the text area to hold all content **/
function textAreaFix() {
  jQuery('textarea').on( 'change keyup keydown paste cut', function (event){
    jQuery(this).height(100);
    jQuery('textarea').each(function() {
      jQuery(this).height(jQuery(this).prop('scrollHeight'));
    });
  });
  return null;
}
/** Function to fix and set the custom date/time picker **/
function setupDates() {
  jQuery('.dateFormat').remove();
  var inputs = jQuery('.inputDate');
  jQuery(inputs).each(function() {
    var input = jQuery(this).val().split('/')[2];
    if(input.length > 4) {
      input = input.split(" ")[0];
    }
    if(input < '2015') {
      jQuery(this).val("");
    }
  });
  console.log("Setup Dates function ran");
  jQuery('.inputDate').datetimepicker();
}

This function is called using the onComplete ajax method. The problem is that when it runs only textAreaFix() is called. In the console only "edit render start" and "edit render middle" show up.

enter image description here

The reason that "Setup Date function ran" first is because I have this function,

jQuery(document).ready(function() {
  jQuery.material.init();
  textAreaFix();
  setupDates();
  tourStep();
  easterEgg();
});

How can I get the setupDates() function called?

EDIT:

I added more debugging to setupDates(),

/** Function to fix and set the custom date/time picker **/
function setupDates() {
  jQuery('.dateFormat').remove();
  var inputs = jQuery('.inputDate');
  console.log(inputs);
  jQuery(inputs).each(function() {
    var input = jQuery(this).val().split('/')[2];
    console.log(input);
    if(input.length > 4) {
      console.log("Input > 4");
      input = input.split(" ")[0];
      console.log(input);
    }
    if(input < '2015') {
      console.log("Fix 2015 dates");
      jQuery(this).val("");
    }
  });
  console.log("Setup Dates function ran");
  jQuery('.inputDate').datetimepicker();
}

When I run this I get,

enter image description here

I am not sure where the "undefined" comes from though.

Upvotes: 0

Views: 113

Answers (1)

James Nearn
James Nearn

Reputation: 171

My guess is that it is displaying undefined because the loop is repeating and jQuery(this).val().split('/')[2] is causing a problem. Maybe console.log on this and this.val() right at the beginning of the loop's block? – James Nearn 30 mins ago

Upvotes: 2

Related Questions