1

I have a table and one row contains an input field. And other row contains td values. I need to compare the value inside the input box with the td values inside table. But the oldDivision value and NewDivision values are alerted same. But not entering to the if condition. How to solve this?

$("#tblGroupItemDetails tbody tr").each(function(i, tr) {
  if (!$(tr).hasClass("tdItemMaster")) {

    var oldDivision = $(tr).find(".txtDivisionName").html().toLowerCase();
    var NewDivision = $(".txtDivisionName").val().toLowerCase();
    alert(oldDivision)
    alert(NewDivision)

    if (oldDivision == NewDivision) {

      isDivisionExist = true;
      alert(" equal")

    } else {
      isDivisionExist = false;
      alert("not equal")
    }
  }


});
5
  • 1
    If you want to compare two objects then you can use following way : if (oldDivision === NewDivision ) { Commented Jun 24, 2019 at 9:40
  • 3
    add .trim() to botth your variables. "x " == "x" will give false Commented Jun 24, 2019 at 9:41
  • 2
    "are alerted the same" - might look it, but they're not the same. Try this alert("[" + NewDivision + "]") (and same for other) Commented Jun 24, 2019 at 9:42
  • 2
    also, if you want to compare text only than use .text(), not .html(). Commented Jun 24, 2019 at 9:42
  • 3
    Thankyou .trim() worked.Space was the issue Commented Jun 24, 2019 at 9:48

1 Answer 1

1

You need to apply .trim() to remove extra spaces from text, if there

var oldDivision = $.trim($(tr).find(".txtDivisionName").html().toLowerCase());
var NewDivision = $.trim($(".txtDivisionName").val().toLowerCase());
Sign up to request clarification or add additional context in comments.

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.