I have a getValue
function which just grabs some numbers from an HTML page.
How can I access those variables in later functions without passing them down the entire chain of functions? As they sit within a function they are not global?
When I call the addition function, it will need to access the variables in the getValue
function.
In Java, I would have used a separate class with a normal getter and setter pattern but unsure how to replicate something similar.
I feel like I am missing a key JavaScript concept here.
function getValue() {
var decimal = parseInt(parent.document.getElementById('CF_9813416').value);
var a = parseFloat(parent.document.getElementById('CF_9814331').value);
var b = parseFloat(parent.document.getElementById('CF_9814407').value);
var c = parseFloat(parent.document.getElementById('CF_9814615').value);
}
function radioButtons() {
for (var i = 0; i < parent.document.Form1.CF_9813571.length; i++) {
if (parent.document.Form1.CF_9813571[i].checked) {
var sign = parent.document.Form1.CF_9813571[i].value;
}
}
radioResult(sign);
}
function radioResult(sign) {
if (sign == '9813524') {
addition();
} else if (sign == '9813527') {
subtraction();
} else if (sign == '9813530') {
multiplication();
} else devision();
}
function addition() {
result = a + b + c;
writeValue(result, decimal);
}
function subtraction() {
result = a - b - c;
writeValue(result, decimal);
}
function multiplication() {
result = a * b * c;
writeValue(result, decimal);
}
function devision() {
result = a / b / c;
writeValue(result, decimal);
}
function writeValue(result, decimal) {
parent.document.getElementById('CF_9814616').value = result.toFixed(0);
parent.document.getElementById('CF_9814617').value = result.toFixed(decimal);
}
</script>