javascript variables and placement of
Greetings! I am at a loss of why this is happening and hopefully someone can give me a bit of insight. I am creating an html form to be used in a mainframe. All validation on the form has to be done in javascript. So, I have this function:
Code:
function getTotalPayoffAmount() {
var j_PayoffTotalRef = document.getElementById('txtPayoffTotal');
j_PayoffTotalRef.value = '';
var j_currentPrincipalBalanceRef = document.getElementById('txtCurrentPrincipalBalance');
var j_accruedInterestRef = document.getElementById('txtAccruedInterest');
var j_currentPrincipalBalanceVal = j_currentPrincipalBalanceRef.value;
var j_accruedInterestVal = j_accruedInterestRef.value;
j_currentPrincipalBalanceVal = parseFloat(j_currentPrincipalBalanceVal.replace(/[^0-9-.]/g, ''));
var j_totalPayoffAmount = j_currentPrincipalBalanceVal + j_accruedInterestVal
j_PayoffTotalRef.value = formatCurrency(j_totalPayoffAmount);
}
There are a lot more variables but my problem is that I have other functions that I created that could use these variables but when I move them out of the function and paste them in between the <script> tags, I get an object is expected error. I copy the varaiables back into the function and boom...it works again...why?!
may be a silly question but it doesn't make sense to me, I'm just creating a reference to an textbox and it's value, so why can't I do that outside of a function and then call those variables from within the function??
Re: javascript variables and placement of
I don't see any reason why you would be getting that error. As long as the variable is declared globally (not in another function) it should be accessible from your function. If you can show the code you modified it to be, something might jump out.
Also, who is entering information into this form? Using JavaScript validation *only* is definitely frowned upon as anyone can disable it and submit the form anyway.
Re: javascript variables and placement of
Here's what I changed it too when I got the error:
Code:
var j_currentPrincipalBalanceRef = document.getElementById('txtCurrentPrincipalBalance');
var j_accruedInterestRef = document.getElementById('txtAccruedInterest');
var j_currentPrincipalBalanceVal = j_currentPrincipalBalanceRef.value;
var j_accruedInterestVal = j_accruedInterestRef.value;
var j_PayoffTotalRef = document.getElementById('txtPayoffTotal');
j_PayoffTotalRef.value = '';
function getTotalPayoffAmount() {
j_currentPrincipalBalanceVal = parseFloat(j_currentPrincipalBalanceVal.replace(/[^0-9-.]/g, ''));
var j_totalPayoffAmount = j_currentPrincipalBalanceVal + j_accruedInterestVal
j_PayoffTotalRef.value = formatCurrency(j_totalPayoffAmount);
}
I realize using javascript ONLY for validation isn't the best idea but with the mainframe system that is being used, javascript is the only way. Even though the form is in html, the mainframe system uses some propriatory (spelling) modules that displays the form so turning off the javascript in the browser has no effect on the mainframe form. I did test this before i started working on the form, just to be on the safe side. :)
Re: javascript variables and placement of
Which line are you specifically getting the error and what is the exact error?
And FYI, turning JavaScript is not the only worry. People using Chrome, Firefox and probably Safari or Opera can modify the JavaScript on the page themselves while accessing your page. With JavaScript completely disabled, I could completely bypass your validation.
Re: javascript variables and placement of
In response to your FYI, I'm in a tightly controlled network where only IE8 is used. I do completely understand what you are saying though and I do appreciate the insight. :)
if I move this line out of the function:
Code:
var j_currentPrincipalBalanceRef = document.getElementById('txtCurrentPrincipalBalance');
I get the error "Object Required" on this line:
Code:
var j_currentPrincipalBalanceVal = j_currentPrincipalBalanceRef.value;
Re: javascript variables and placement of
Where exactly have you declared the variables?
My best guess is that you're declaring "j_currentPrincipalBalanceRef" before the element with the ID "txtCurrentPrincipalBalance" is drawn to the screen. The reason it would work in the function is that the function is being called after that element is drawn, whereas when you declare the variable at the global scope, it gets called sequentially.
Try declaring those variables at the bottom of your page in a script tag, rather than in the header.