1

I have the following:

var NewCount = document.getElementById('MainContent_gv_NewCount_' + rowIndex).value;
if (NewCount != "") {
    document.getElementById('MainContent_gv_lblTotal_' + rowIndex).innerHTML = "£" + ((originalCount - NewCount) * unitCost).toFixed(2);
} else { 
   document.getElementById('MainContent_gv_lblTotal_' + rowIndex).innerHTML = "";
            }

The calculations I am doing are based on the value in the textbox. (NewCount).

I want the label to update if the value is any number (including 0), but to be wiped if the user clears the textbox. However, at the moment it is treating a blank textbox and a textbox with 0 in it the same.

How can I differentiate between the two?

3
  • 1
    I don't believe that the code you posted could possibly be responsible for what you perceive to be going wrong. The comparison you've got, NewCount != "", will be true when the value of the input box is any non-empty string, including the string "0".
    – Pointy
    Feb 4, 2011 at 13:48
  • Are you wanting to treat white space the same as "" ? Feb 4, 2011 at 13:53
  • @Pointy - That's not how != works. You need !==.
    – OrangeDog
    Feb 4, 2011 at 14:18

4 Answers 4

3

Use !== in your if statement.

3
1

I can't reproduce the behavior you are describing. In my tests a textbox with "0" in it will be considered not blank by Javascript using your comparison logic (!= "").

Here is my attempt: http://jsfiddle.net/pJgyu/5404/

0

Any of the following could work

NewCount.length > 0
NewCount !== ''
0
if ( NewCount.length == 0 ) {
    // text-box is empty
} else if ( !isNaN(NewCount) ) {
    // the value is a number
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.