radleybobins
radleybobins

Reputation: 981

jquery if statement not functioning correctly

I was writing a very simple function but can't get the if statement to work correctly.

$("#supplyRequestTextbox-clients").live("blur", function() {
    var textboxValue = $(this).val().replace(/\d+/g, '').replace(" ", "");
    if (textboxValue == "NO ACCOUNT") {
        $(".controlBarTextboxNoAccount").fadeIn("fast");
    }
});

The value in the input #supplyRequestTextbox-clients represents a client and is organized like this:
id# FirstName LastName email phone An example: 000001 John Doe [email protected] 123-456-7890

When no account is found, the string appears exactly like this: 000906 NO ACCOUNT

In my function I strip the digits and first space, then check to see if it works in my if statement. I have alerted textboxValue and it is passing correctly, but no matter that textboxValue is, the if statement never fires.

Upvotes: 0

Views: 94

Answers (5)

Francis Kim
Francis Kim

Reputation: 4285

  1. Use == for comparisons as Kolink suggest.

  2. You are replacing " " with "", IE. removing all spaces so your if statement will never return true.

Upvotes: 0

Jim O'Connell
Jim O'Connell

Reputation: 81

Try doing this:

$("#supplyRequestTextbox-clients").live("blur", function() {
    var textboxValue = $(this).val().replace(/\d+/g, '').replace(" ", "");

    // check what textboxValue evaluates to:
    alert(textboxValue);

if (textboxValue == "NO ACCOUNT") {
    $(".controlBarTextboxNoAccount").fadeIn("fast");
}
});

Upvotes: 0

Simon M
Simon M

Reputation: 2941

Change the if condition to:

if(textboxValue.indexOf("NO ACCOUNT") !== -1)

indexOf("NO ACCOUNT") finds "NO ACCOUNT" within textboxValue, if it can't find it -1 is returned. So this will be true if "NO ACCOUNT" is anywhere in your string.

Upvotes: 4

Claudio Redi
Claudio Redi

Reputation: 68400

Change your if condition to

if (textboxValue == "NO ACCOUNT")

If you do

if (textboxValue = "NO ACCOUNT")

You're actually assigning "NO ACCOUNT" to textboxValue and evaluating the result as bool.

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324600

Use == for comparisons. You are assigning a truthy value, so the statement always fires.

Upvotes: 2

Related Questions