beatmusic67
beatmusic67

Reputation: 35

if else statement not working as expected

Would appreciate any help to a newbie here. I have a simple if/else if statement that for some reason only gets to the second condition, even if the third is met. I've tried adding else to the end, tried pretty much everything but still nothing. I know it's not working, because I added the alert triggers and no matter what it never gets to the third condition. What am I missing here? I've looked around a lot so I'm sorry if I'm beating a dead horse here but I couldn't find a solution. I should note that I'm trying to get the conditions on the change of a selection drop-down box. Thanks!!!

My code:

$('#table_two select:eq(6)').change(function(e){

var allDiv = $('#table_two,#table_three,#table_four');
var parBoxOne = $('#par_one').val();

var totalOne = 1 - parBoxOne;
var totalTwo = 2 - parBoxOne;


if ($(this).val() == "made_shot"){
$(allDiv).hide();
$('#score_one').val(totalOne);
$('#score_total').val($('.score_input').val());
alert('ACE!!');
}

else if ($(this).val() == 'green', 'trees', 'fairway', 'rough', 'sand' ){
$('#score_one').val(totalOne);
$('#score_total').val($('.score_input').val());
$('#table_two').hide();
$('#butt_two').html('Show stroke');
alert('triggered two');
}

else if ($(this).val() == 'other', 'penalty_water', 'penalty_ob', 'penalty_sand' ){
$('#score_one').val(totalTwo);
$('#score_total').val($('.score_input').val());
$('#table_two').hide();
alert('triggered three');
}

});

Upvotes: 0

Views: 217

Answers (3)

Ram
Ram

Reputation: 144659

you cannot campare strings in this way, you can use $.inArray() function instead and create an array of values:

jQuery.inArray( value, array [, fromIndex] )

 if($.inArray($(this).val(), ['green', 'trees', 'fairway', 'rough', 'sand']) != -1) {

 }

Upvotes: 0

nbrooks
nbrooks

Reputation: 18233

You cant combine multiple conditions in an if statement using commas:

if ($(this).val() == 'green', 'trees', 'fairway', 'rough', 'sand' )

Instead, you can make the value list into an array and check if your value is in the array:

var stuff = ['green', 'trees', 'fairway', 'rough', 'sand'];
if ( $.inArray($(this).val(), stuff ) >= 0 )

To combine multiple conditions in an if, the syntax is this (much longer than above):

if ($(this).val() == 'green' || $(this).val() == 'trees' || ... )

Upvotes: 5

sabithpocker
sabithpocker

Reputation: 15558

Just another apporoach:

switch($(this).val()){
    case 'made_shot':
        $(allDiv).hide();
        $('#score_one').val(totalOne);
        $('#score_total').val($('.score_input').val());
        alert('ACE!!');
        break;

    case 'green':
    case 'trees':
    case 'fairway':
    case 'rough':
    case 'sand':
        $('#score_one').val(totalOne);
        $('#score_total').val($('.score_input').val());
        $('#table_two').hide();
        $('#butt_two').html('Show stroke');
        alert('triggered two');
        break;

    case 'other':
    case 'penalty_water':
    case 'penalty_ob':
    case 'penalty_sand':
        $('#score_one').val(totalTwo);
        $('#score_total').val($('.score_input').val());
        $('#table_two').hide();
        alert('triggered three');
        break;
    }

Upvotes: 1

Related Questions