0

I have a jquery Function in which i have declare a variable and make it default false.and then in the in the If condition i am assigning true value to that variable.But i am always getting false..why is so?? When used alert i am getting 'true' butin the if condition it is 'false'

Here is my Code:

var abc = false
$('#dataTable tr td').find("input[name='" + 'lstDetlIssue[' + i + '].Product' + "']").change(function () {
abc = true;
alert(abc);
});

if (MstCopyParseData[rowIndex].ProductID == MstCopyParseData[parseInt(i)].ProductID && (i != rowIndex && (MstCopyParseData[i].Product != "" && abc == true ))) {
2
  • if possible give us a fiddle jsfiddle.net Commented Feb 20, 2017 at 5:40
  • can you give a example link to check what actually happening.So that we can help? Commented Feb 20, 2017 at 5:41

3 Answers 3

1
    $('#dataTable tr td').find("input[name='" + 'lstDetlIssue[' + i + '].Product' + "']").change(function () {
    abc = true;
    alert(abc); // true here
    });

false here

ABC is true inside this one but it's false outside of this function because you set it to false by default... thats' why u getting false.... if you want true for abc then you should put ur "if" condition inside of above function

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

Comments

0

It might be a problem with scoping. Its hard to tell from the code you gave but try doing something like this:

// create a variable that refers to the scope you want.
const that = this;
let abc = false;

$('#dataTable tr td').find("input[name='" + 'lstDetlIssue[' + i + '].Product' + "']").change(function () {
    // then change it like so
    that.abc = true;
    alert(that.abc);
});

This youtube video explains javascript scope pretty well.

2 Comments

i Am not able to open the link
also you should restructure your code a bit. I would suggest creating a function to put your if statement in and inside the change callback, call that function. The value of your function at the time code runs will be false.
0

You set "abc" as true when you change a input value, but your "if" condition run when you just set the "change" function, at that time, your "abc" is just set as false.

if you want your "abc" to be true, move the "if" condition into the .change()

var abc = false
$('#dataTable tr td').find("input[name='" + 'lstDetlIssue[' + i + '].Product' + "']").change(function () {
  abc = true;
  alert(abc);
  if (MstCopyParseData[rowIndex].ProductID == MstCopyParseData[parseInt(i)].ProductID && (i != rowIndex && (MstCopyParseData[i].Product != "" && abc == true ))){
    //TODO
  }
});

3 Comments

No.if i move my if Condition inside chage event it wont work.
you have "MstCopyParseData[rowIndex].ProductID == MstCopyParseData[parseInt(i)].ProductID && (i != rowIndex && (MstCopyParseData[i].Product != "" " before abc == true, if this if condition is false, it doesn't mean "abc" is not true, maybe one of these three expression is false. You can write as if(abc == true) to judge every expression to find out which of the expression is false.
if (a && b) is true, it means a is true and b is true.

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.