IF Statement Always True

I have a radwindow that I use to show error messages to users in an application.
My goal is as below;
If the message is not a warning/error I want user to be redirected when they click "OK" on the popped up radwindow. To accomplish this, I'm setting HiddenField value to "1" when the operation is successful and to "0" when the operation fails. My problem is that when I check the HiddenField value on the client-side the IF statement always returns true, and the page is redirected.

Here are the cases when I set the hiddenfield value and set the radwindow message;

if(x)
{
   hfPasswordWarning.Value = "0";
   ShowMessage(MessageResource.ChangePasswordAuthenticateError,false);
}

else
{
   hfPasswordWarning.Value = "1";
   ShowMessage(MessageResource.ChangePasswordSuccess,true);
}

And the client-side code which I'm having issues with, IF statement is always true;

var hv = $('#hfPasswordWarning').val();

    if (hv.val = "1") {
        window.location = "../Main/Login.aspx";
    } else {
        return false;
    }

I've added an Alert("xx") to check if it ever gets into else statement but It doesn't. I've tried to simplify the explanation of my problem as much as I can. Thank you for your understanding.

Answers:

Answer

you wrote

if (hv.val = "1")

but this is an assignment (evaluated as true, in your code): check for equality is == (equality by value) or === (equality by value and type)

Answer

you have used = operator which is used for assignment for comparison == operator is used try this In your if statement try this

if(hv.val=="1")
Answer

You need to use == or === for comparison instead of =.

  • x = y assigns y to x
  • x == y checks if y and x are loosely equal
  • x === y checks if y and x are equal and of the same type

So what you need to do is replacing if (hv.val = "1") with if (hv.val == "1")

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.