Here is my function:
function reCalculate(i) {
document.getElementById("Q" + i).value = document.getElementById("C" + i).value - document.getElementById("QA" + i).value;
if (document.getElementById("Q" + i).value < 0) {
document.getElementById("Q" + i).value = 0;
}
if (document.getElementById("Q" + i).value < document.getElementById("E" + i).value && document.getElementById("Q" + i).value != 0) {
alert(document.getElementById("Q" + i).value + " is less than " + document.getElementById("E" + i).value + "?");
document.getElementById("Q" + i).value = document.getElementById("E" + i).value;
}
document.getElementById("Q" + i).value = Math.ceil(document.getElementById("Q" + i).value);
}
It checks Q, if it's less than 0, it makes it 0. Then, if it's not 0, but it's less than E, it makes it E. For some reason this function works UNLESS Q is a double digit number.
For example, if Q is 7 and E is 2, then it will leave Q at 7. However, if Q is 10 and E is 2, for some reason it thinks that 10<2, and it changes Q to 2!
Am I missing something here??
Here is my function:
function reCalculate(i) {
document.getElementById("Q" + i).value = document.getElementById("C" + i).value - document.getElementById("QA" + i).value;
if (document.getElementById("Q" + i).value < 0) {
document.getElementById("Q" + i).value = 0;
}
if (document.getElementById("Q" + i).value < document.getElementById("E" + i).value && document.getElementById("Q" + i).value != 0) {
alert(document.getElementById("Q" + i).value + " is less than " + document.getElementById("E" + i).value + "?");
document.getElementById("Q" + i).value = document.getElementById("E" + i).value;
}
document.getElementById("Q" + i).value = Math.ceil(document.getElementById("Q" + i).value);
}
It checks Q, if it's less than 0, it makes it 0. Then, if it's not 0, but it's less than E, it makes it E. For some reason this function works UNLESS Q is a double digit number.
For example, if Q is 7 and E is 2, then it will leave Q at 7. However, if Q is 10 and E is 2, for some reason it thinks that 10<2, and it changes Q to 2!
Am I missing something here??
Share Improve this question edited Dec 27, 2012 at 15:27 Cerbrus 72.9k19 gold badges136 silver badges150 bronze badges asked Dec 27, 2012 at 15:15 fullOfQuestionsfullOfQuestions 4531 gold badge11 silver badges25 bronze badges 2- 3 Please cache your DOM elements to variables. Using getById each time reaches back into the DOM and is costly in performance. – rlemon Commented Dec 27, 2012 at 15:17
- Good point, I fixed that, thanks! – fullOfQuestions Commented Dec 27, 2012 at 15:48
4 Answers
Reset to default 10When you pull the .value
of an element it returns a string. '10'<'2'
will return true.
You can simply do a parseInt/parseFloat on the value, ala
var q = parseInt(document.getElementById("Q"+i).value,10)
Thats because it is considering your Q
as a string while paring.
Try the following instead:
function reCalculate(i){
var Z = document.getElementById, P = parseInt;
var qElem = Z("Q"+i);
var q = P(qElem.value, 10);
var c = P(Z("C"+i).value, 10);
var qa = P(Z("QA"+i).value, 10);
var e = P(Z("E"+i).value, 10);
q = c - qa;
if (q < 0) qElem.value = 0;
if (q < e && q != 0){
alert(q+" is less than "+e+"?");
qElem.value = e;
}
qElem.value = Math.ceil(q);
}
May be you should do a
parseFloat(document.getElementById("Q"+i).value)
to make sure you are paring numbers
You are paring strings not numbers. Use the unary +
to convert to a number:
if (+document.getElementById("Q" + i).value < +document.getElementById("E" + i).value ...)
You should use variables by the way:
var input_one = document.getElementById("Q" + i).value,
input_two = document.getElementById("E" + i).value;
if (+input_one < +input_two) {
}