AlexNasonov
AlexNasonov

Reputation: 607

JS if/else trouble

I have a simple code:

<script type="text/javascript">
    jQuery(document).ready(function() {
        jQuery("#continue").click(function() {
        var value = jQuery('#continue').attr('value')
            alert (value);
            if (value='next'){
                jQuery('#msg_title').html('blablabla');
                jQuery('#msg').html('blablabla'');
                jQuery('#continue').attr('value','removeConfig');
                value = jQuery('#continue').attr('value');
            }
            else if (value='removeConfig') {
                jQuery('#msg_title').html('It Works!');             
            }
            else {
                alert ('Something wrong')
            }
        return false;
        });
});

</script>

It works well in firs if phase, changes button's value (as I see from alert), but doesn't make else if and else statements.

Upvotes: 0

Views: 125

Answers (4)

Jasper
Jasper

Reputation: 75993

You need double equals signs as singles will return true in this case:

if (value =="next")

Single = is an assignment operator and double == is a comparison operator. Since you were using an assignment operator and setting the variable to a non falsy value the first if statement resolved to true.

Upvotes: 2

Ryan Amos
Ryan Amos

Reputation: 5452

Use ==, not =. A single = is an assignment operator, which means it's assigning "next" to value, then testing if value is false, not if value equals next

Upvotes: 2

Joe
Joe

Reputation: 82584

You need ==

(value='next'){

should be:

(value == 'next'){

You are testing if ('next') { which is always true. Only string the evaluates to false is empty string, ""

Upvotes: 2

maxedison
maxedison

Reputation: 17553

Your comparison operator is wrong: if (value='next') should be if (value == 'next'){ or if (value === 'next'){.

Note the extra = signs.

Upvotes: 9

Related Questions