Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8689251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:35:55+00:00 2026-06-12T23:35:55+00:00

I have been working for some time on an application form for an insurance

  • 0

I have been working for some time on an application form for an insurance company. They sell travel insurance.

As an additional small feature they have asked me to take the dates the depart and return dates the user inputs, calculate the number of days between them and then display number in the ‘days’ box. The goal of which is so that the user can enter the two dates and have the number of days auto-calculated.

I have already created a function which properly calculates the date and I have tested it using manually assigned date values.

My issue has come up when using the JS calendar.

What I tried to do was use the onblur of the second box to access the function and spit the date out into the ‘days’ box. I quickly realized that the onblur is triggered before the code for the JS calendar puts in the date, hence there is no date for the function and the function does not run.

I then tried to use onchange and realized it would not work either because the user is not actually changing the date, code is.

So what I tried to do next was use an Interval to trigger the function, this is where I have run into issues.

Below is my code in my caldate.js file which is attached to my HTML form.

    var namestart = new Array ();
namestart[0] = "trav_emer_single_date_go";
namestart[1] = "trav_emer_extend_date_go";
namestart[2] = "allinc_single_date_go";
namestart[3] = "allinc_annual_date_go";
namestart[4] = "cancel_date_go";
namestart[5] = "visitor_supervisa_date_go";
namestart[6] = "visitor_student_date_go";
namestart[7] = "visitor_xpat_date_go";

var namend = new Array ();
namend[0] = "trav_emer_single_date_ba";
namend[1] = "trav_emer_extend_date_ba";
namend[2] = "allinc_single_date_ba";
namend[3] = "allinc_annual_date_ba";
namend[4] = "cancel_date_ba";
namend[5] = "visitor_supervisa_date_ba";
namend[6] = "visitor_student_date_ba";
namend[7] = "visitor_xpat_date_ba";

var names = new Array ();
names[0] = "trav_emer_single_days";
names[1] = "trav_emer_extend_days";
names[2] = "allinc_single_days";
names[3] = "allinc_annual_days";
names[4] = "cancel_days";
names[5] = "visitor_supervisa_days";
names[6] = "visitor_student_days";
names[7] = "visitor_xpat_days";


    function daysBetween() {

for (var i = 0; i < 8; i++) {   

//Get the value of the current form elements
var start = document.getElementById(namestart[i]).value;
var end = document.getElementById(namend[i]).value;

//Duration of a day
var d = 1000*60*60*24;

// Split Date one
var x = start.split("-");
// Split Date two
var y = end.split("-");

/// // Set Date one object
var d1 = new Date(x[0],(x[1]-1),x[2]);
//  // Set Date two object
var d2 = new Date(y[0],(y[1]-1),y[2]);
//  
//  //Calculate difference
diff = Math.ceil((d2.getTime()-d1.getTime())/(d));

//Show difference
document.getElementById(names[i]).value = diff;

}
}

function interval() {
var int = setInterval(function(){daysBetween()},500);
}

The list of arrays at the beginning is the names of the elements which I need to access. What I intend to do with my function is on each interval run through a loop which checks all 8 of these elements. namestart[] and namend[] are the start and end dates entered by the user. names[] lists the names of the boxes where days are to be displayed.

I have not been able to even test the interval portion because I can’t even get the daysBetween() to run once with manually assigned value=”date” for testing purposes, it just won’t run at all.

The issue is quite simple really: document.getElementById(namestart[i]).value isn’t pullig out a value, it just hangs the script because it can’t find the value. I have also tried using the form_name.elements().value notation and the form_name.element_name.value notation to no avail.

I am really stumped here as far as I can tell the code should be working, I give a list of the names of the items and I tell the script to access them using – what I have used, and seen many times to work getElementByID.

Any assistance would be greatly appreciated as I am not quite sure where to go from here.

As requested here is a JS fiddle link: http://jsfiddle.net/L2H9N/ – pure JS no libraries.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:35:57+00:00Added an answer on June 12, 2026 at 11:35 pm

    What I think is happening is your daysBetween function is executing before the DOM is ready, which is throwing an error and nuking the rest of your javascript. To fix it, you’ll need to put the call to daysBetween into a callback for window.onload or attach it to a callback in your calender.

    It would also be a good idea to coalesce your nulls or at least check for them before proceeding to do calculations on the variables.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one web application which I have been working for some time.I am
I have been working on a first application for some time based on the
I have been working on a Qt application for some time now, and after
I have been working on this for some time now and just cant quite
I have been working in C++ for some time now, but I am unsure
I have been working on this for some time. The script does work and
I've been working on a guitar tuner Java application for quite some time and
Scenario: I have been using Java SE for quite some time, working with threads
I have been working with Struts for some time, but for a project I
I have been working with some deadlocks situations in our application recently and have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.