Q:

javascript "less than" if statement failing

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??

A:

When 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)

A:

Thats because it is considering your Q as a string while comparing.
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);
}

