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;
}
nem035
  • 34,790
  • 6
  • 87
  • 99
  • 1
    `txtcurrentrcvamount` appears to be a jQuery object, not a string or number. What is the purpose of `for` loop? – guest271314 Jan 23 '18 at 03:31
  • how can i get the value and compare with cell – user3510330 Jan 23 '18 at 03: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? – guest271314 Jan 23 '18 at 03:34
  • NO cell value will be different in each iteration. – user3510330 Jan 23 '18 at 03:37
  • take the `txtcurrentrcvamount` out from for loop. Everything will be okay. – AA Shakil Jan 23 '18 at 03:38
  • Your current code uses only a single element. Are you trying to pass `i` to `.eq()`? `.find("tr:eq(" + i + ")")` ? – guest271314 Jan 23 '18 at 03:39
  • Showing same Output. var txtcurrentrcvamount = $("input[id*=txtrcvQuantity]").val() var cell = $("#gvGoodReceived").find("tr:eq(" + i + ")").find("td:eq(2)").text(); – user3510330 Jan 23 '18 at 03:49
  • `.val()` returns the first element of the collection returned by `jQuery()` which means `txtcurrentrcvamount` will also be defined as the same value at each iteration of the `for` loop – guest271314 Jan 23 '18 at 03:54
  • How can i manage it? – user3510330 Jan 23 '18 at 03:56
  • Without a `for` loop `var res = $.map($("input[id*=txtrcvQuantity]"), function(el, index) { return +el.value > +$("#gvGoodReceived").find("tr:eq(" +index+ ")").find("td:eq(2)").text() }); if (res.some(function(bool) { return bool })) { return false }` – guest271314 Jan 23 '18 at 04:00

2 Answers2

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

nem035
  • 34,790
  • 6
  • 87
  • 99
  • The `>` operator coerces string to number, yes? – guest271314 Jan 23 '18 at 03:38
  • 1
    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`. – nem035 Jan 23 '18 at 03:41
  • 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? – guest271314 Jan 23 '18 at 03:42
  • 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. – nem035 Jan 23 '18 at 03:45
  • Was curious about that portion of your Answer relating to comparing numbers as strings. Your mention of lexicographic comparison furthered that initial curiosity. There are other issues with OP's code. `var cell = $("#gvGoodReceived").find("tr:eq(0)").find("td:eq(2)").text();` would be the same value for each iteration, though OP states that a different element defined as `cell` for each iteration is expected https://stackoverflow.com/questions/48393756/if-condition-in-javascript-function-not-working/48393779?noredirect=1#comment83775264_48393756. – guest271314 Jan 23 '18 at 03:49
  • 1
    @guest271314 Actually, a simple example would be `'10' > '9' === false` – nem035 Jan 23 '18 at 03:50
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;
}
A.D.
  • 2,352
  • 2
  • 15
  • 25