Oninput JavaScript Date Function Calculator
I found this code online and cant get it to work correctly, can someone tell me where am I going wrong? In the description it looked like it would work exactly how I needed it, but for some reason I can't get it to work correctly on my end. I'm making a bunch of warnings and can't even display them. I'm sorry that I'm trying really hard to teach myself JavaScript.
Enter Button:
<cfinput
type="datefield"
name="purchasedate"
width="130"
required="yes"
message="Please enter purchase date."
value="#dateformat(now(),"mm/dd/yyyy")#"
oninput="calculateTotal();"
>
JavaScript function:
function getDatePrice(date) {
var datePrice = 0
var theForm = document.forms["form"];
var purchasedate = theForm.elements["purchasedate"];
var date = new Date(purchasedate.value);
if (Object.prototype.toString.call(date) !== '[object Date]') {
date = new Date();
}
var today = new Date();
var diffMilli = today - date;
var diffDays = diffMilli * 1000 * 60 * 60 * 24;
if (diffDays > 30) {
datePrice= 20;
}
return datePrice;
}
function calculateTotal()
{
var titleFees = getDatePrice(date);
var divobj = document.getElementById('totalPrice');
divobj.style.display='block';
divobj.innerHTML = "Estimated Transfer Fees $"+titleFees;
}
+3
user4122820
source
to share
1 answer
function getDatePrice() { // Don't pass any arguments ..*
var theForm = document.forms.form; // Use rather dot notation
var purchasedate = theForm.elements.purchasedate; // Use rather dot notation
var date = new Date(purchasedate.value); // *.. cause you'll get here the value
if (Object.prototype.toString.call(date) !== '[object Date]') {
date = new Date();
}
var today = new Date();
var diffMilli = today - date;
// So today - date = Some time in milliseconds...
// if you multiply that value you'll get an astronomic number so, to get days..
var diffDays = Math.floor(diffMilli / 1000 / 60 / 60 / 24); // ..Divide!
var datePrice = 0;
if (diffDays > 30) {
datePrice = 20;
}
return datePrice;
}
function calculateTotal(){
var divobj = document.getElementById('totalPrice');
divobj.style.display = 'block';
divobj.innerHTML = "Estimated Transfer Fees $"+ getDatePrice();
}
and for the input event use oninput="calculateTotal();"
+1
source to share