0

I need to compare the values and return the message.But the message returned always. How can i do it?

Javascript:

function Calculation() {
  var grid = document.getElementById("<%=gvGoodReceived.ClientID%>");
  for (var i = 0; i < grid.rows.length - 1; i++) {
    var txtcurrentrcvamount = $("input[id*=txtrcvQuantity]")

    var cell = $("#gvGoodReceived").find("tr:eq(0)").find("td:eq(2)").text();
  }
  if (txtcurrentrcvamount > cell) {
    alert("Receive quantity must be less or equal PO quantity");
    return false;
  }
  return true;
}

10
  • 1
    txtcurrentrcvamount appears to be a jQuery object, not a string or number. What is the purpose of for loop? Commented Jan 23, 2018 at 3:31
  • how can i get the value and compare with cell Commented Jan 23, 2018 at 3:33
  • See the Answer to your Question, get the .value of the element, or using jQuery, .val(). Is the expected result for cell to be the same value at each iteration? Commented Jan 23, 2018 at 3:34
  • NO cell value will be different in each iteration. Commented Jan 23, 2018 at 3:37
  • take the txtcurrentrcvamount out from for loop. Everything will be okay. Commented Jan 23, 2018 at 3:38

2 Answers 2

3

You need to take the value of your input:

var txtcurrentrcvamount = $("input[id*=txtrcvQuantity]").val()
//                                                      ^^^^^^

Since you're comparing numbers, and val() and text() return strings, you should convert your values to numbers before doing the comparison:

if (Number(txtcurrentrcvamount) > Number(cell))

Do note that Number(someStringThatIsNotANumber) will return NaN

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

6 Comments

The > operator coerces string to number, yes?
Not all the time. Only if one of the operands is a number. If both are strings it performs lexicographical comparison i.e. 'b' > 'a' === true.
Can you post an example or link to a Question/Answer where comparing numbers as strings using > or < operator does not return the expected result?
No, but that's not the point. The point was to not give false positives like 'b' > 'a'. I guess we can do more explicit type checks but I didn't want to overwhelm the OP since it is obvious this is a beginner's question. It just seemed this was a simple yet helpful answer. I don't mind making a change if you have a suggestion.
@guest271314 Actually, a simple example would be '10' > '9' === false
|
1

Because your scope of a variable (txtcurrentrcvamount) is limited in between for loop, That's why this not working outside the loop scope.

for more detail, you can view this post...scope of variables

For using this variable in if condition you have initialized it before the for loop...

EDIT: Try this may this help you either. I think there some other finding to suppose you have two rows in your grid then which row value you want to check because this always return last row value... and if there a number value for both of the variable assignment txtcurrentrcvamount ,cell then it should be work perfectly.

function Calculation() {
  var grid = document.getElementById("<%=gvGoodReceived.ClientID%>");
  var txtcurrentrcvamount ;
  var cell;
  for (var i = 0; i < grid.rows.length - 1; i++) {
    txtcurrentrcvamount = $("input[id*=txtrcvQuantity]").val();
    cell = $("#gvGoodReceived").find("tr:eq(0)").find("td:eq(2)").text();

  }
  if (Number(txtcurrentrcvamount) > Number(cell)) {
    alert("Receive quantity must be less or equal PO quantity");
    return false;
  }
  return true;
}

1 Comment

i declared (txtcurrentrcvamount) variable out of the loop but it's not working in case of my condition.

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.