0

I am getting a value from a div & trying to compare with if condition but every-time execute in wrong way, Why ? please help me

example:W.r.t my code i got the totalcount value as 2, but again it is going into the if block , it should not go

My code:

var totalcount=$("#itemLocationGrid").jqGrid('getGridParam', 'records');//gives value 2
 Number(totalcount);

 if(totalcount===0);
   {  //every time it executes. why?
    alert("No item locations found to perform this ADD action."); 
    return false;
   }
5
  • 1
    What do you think parseInt(0) does? parseInt is to parse a string into a number. 0 is already a number. Commented Jan 23, 2014 at 8:07
  • @T.J.Crowder is right. Also, you just said it gives 2. Your if statement, even if it was only totalcount == 0 would never hit? Commented Jan 23, 2014 at 8:07
  • cant you read the error in console ? Commented Jan 23, 2014 at 8:08
  • @RGraham: The OP's question is, why is it going into the block even though the value is 2 and they're testing for 0. Commented Jan 23, 2014 at 8:10
  • remove Number(totalcount); cause it will return as int and remove semicoln(;) after if Commented Jan 23, 2014 at 8:36

3 Answers 3

5

It's the semicolon...

if(totalcount===parseInt(0)  || totalcount == "0" || totalcount == 0);
// ...here ----------------------------------------------------------^

That semicolon terminates the if statement; what follows it is a block that is not conditional on the if. You want to remove it so the block is associated with the if.


If you want a number in totalcount and jqGrid returns the value as a string (I don't know that it does, but if it does), and in particular you want a whole number ("integer"), here's how you get it:

var totalcount = parseInt($("#itemLocationGrid").jqGrid('getGridParam', 'records'), 10);
// Parse it -----^ and use a radix (number base) ---------------------------------^^^^

Having done that (or if jqGrid gives you a string), this is how you compare it:

if (totalcount === 0)
{
    // Do something because it's zero
}

Using == will also work. The difference is that === will not coerce types to make things match, == will (e.g., "0" == 0 is true but "0" === 0 is false).

Sign up to request clarification or add additional context in comments.

3 Comments

I like how we were all distracted by the odd parseInt call, but there was a semicolon at the end making it all null and void.
If totalcount can be a string '0' then OP can write if(0 == +totalcount)
@GrijeshChauhan: If totalcount is a string, the OP can also just write if (0 == totalcount), the JavaScript engine will coerce it automatically. Doesn't mean it's a good idea. If you know the number is in a specific number base, it's almost always best to tell the engine what number base you expect, esp. with engines that add octal handling and thus misunderstand (and reject) "08".
1

You need to remove the semicolon for if condition. also you do not need using number. totalcount you get is already in integer format as you are getting the record (See this).Code will look something like this.

var totalcount=$("#itemLocationGrid").jqGrid('getGridParam', 'records');//gives value 2
if(totalcount===0)
  {  //every time it executes. why?
   alert("No item locations found to perform this ADD action."); 
   return false;
 } 

Comments

0

Try this:

No need of other conditions

 if(totalcount===parseInt(0)  || totalcount == "0" ||)

Your code:

if(totalcount == 0)// also remove ; here
       { 
        alert("No item locations found to perform this ADD action."); 
        return false;
       }

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.