Jennifer
Jennifer

Reputation: 1331

Logical OR not functioning

Is there something wrong with the code? Because, when I am removing the OR in the statement, I am getting the right output, but when I'm inserting the || the output that I'm getting is what's in the statement where the OR operator is located.

 $(document).ready(function(){
        $("#select1").change(function(){
            if($(event.target).val() == 'AK'){
                    $("#select2").change(function(){
                        if($(event.target).val() == 'LLC'){
                            $('#txt6').val('250.00');
                        }else{
                            $('#txt6').val('250.00');
                        }
                    }); 
                }else if($(event.target).val() == 'AR'  || 'CO' || 'HI' || 'IA' || 'MS'){
                    $("#select2").change(function(){
                        if($(event.target).val() == 'LLC'){
                            $('#txt6').val('50.00');
                        } else{
                            $('#txt6').val('50.00');
                        }
                    });
            }else
            if($(event.target).val() == 'AZ'){
                    $("#select2").change(function(){
                        if($(event.target).val() == 'LLC'){
                            $('#txt6').val('50.00');
                        } else{
                            $('#txt6').val('60.00');
                        }
                    });
                }else
}else
        if($(event.target).val() == 'WI'){
                $("#select2").change(function(){
                    if($(event.target).val() == 'LLC'){
                        $('#txt6').val('103.00');
                    } else{
                        $('#txt6').val('103.00');
                    }
                });
            }else
        {
            $("#select2").change(function(){
                    if($(event.target).val() == 'LLC'){
                        $('#txt6').val('186.00');
                    } else{
                        $('#txt6').val('186.00');
                    }
                });
        }
    });
});

Upvotes: 0

Views: 46

Answers (4)

Sheepy
Sheepy

Reputation: 18005

|| joins separate and independent boolean conditions, not string values.

You may want to do this instead:

if ( /^(AR|CO|HI|IA|MS)$/.test( $(event.target).val() ) )

Upvotes: 0

Chankey Pathak
Chankey Pathak

Reputation: 21666

$(event.target).val() == 'AR'  || 'CO' || 'HI' || 'IA' || 'MS'

Should be

$(event.target).val() == 'AR'  || $(event.target).val() == 'CO' || $(event.target).val() == 'HI' || $(event.target).val() == 'IA' || $(event.target).val() == 'MS'

That's a small mistake people do, they think correctly but write wrong. Because normally we think:

if value equals to xyz or pqr

and we write

if(value == "xyz" || "pqr")

which is wrong, we did this mistake because we were in flow of words. It should be

if(value == "xyz" || value == "pqr")

instead.

Upvotes: 2

Ed Heal
Ed Heal

Reputation: 60007

This line

 }else if($(event.target).val() == 'AR'  || 'CO' || 'HI' || 'IA' || 'MS'){

is in error.

Need something like

}else if($(event.target).val() == 'AR'  || $(event.target).val() == 'CO' || $(event.target).val() =='HI ...

Upvotes: 0

casraf
casraf

Reputation: 21694

You can't do this:

$(event.target).val() == 'AR'  || 'CO' || 'HI' || 'IA' || 'MS'

As it translates to this:

($(event.target).val() == 'AR')  || ('CO') || ('HI') || ('IA') || ('MS')

Which translates to this:

(<true/false>)  || true || true || true || true

What you need is:

$(event.target).val() == 'AR'  ||
$(event.target).val() == 'CO' || 
$(event.target).val() == 'HI' || 
$(event.target).val() == 'IA' || 
$(event.target).val() == 'MS'

Alternatively, you can do this:

var allowed = ['AR', 'CO', 'HI', 'IA', 'MS'];

if ($.inArray($(event.target).val(), allowed)) {...}

Upvotes: 3

Related Questions