Simple if statement giving me troubles


$('fieldset#actions input[name="whatNow"]').each(function() {
            if($(this).val() != 'Print Parts Card' || $(this).val() != 'Back') {
                $(this).attr('disabled', 'disabled');
            }
})

if I remove the

 || $(this).val() != 'Back'

It works fine, where have I gone wrong?

Try breaking down the condition so that you can test individual parts.

For example:


var printParts = ($(this).val() == 'Print Parts Card');
var back = ($(this).val() == 'Back');
alert('not back ' + !back);
if(!printParts || !back) {

The fieldset contains multiple submit buttons, one will always have the value of “Print Parts Card” and depending on the page state, may also include one with a value of “Back”

You may wish to investigate whether the following two conditions mean the same thing.

(!printParts || !back)

(!(printParts && back))

Try disabling everything first, and then selectively enabling afterwards.


$('fieldset#actions input[name="whatNow"]').each(function() {
    $(this).attr('disabled', 'disabled');
    if($(this).val() == 'Print Parts Card' || $(this).val() == 'Back') {
        $(this).removeAttr('disabled');
    }
});

And I just realised, your original condition may well should have used && instead of ||

if(!partsCard)

and

 if(!back)

work independently, but not when combined

if(!partsCard || !back)

Also

if(!printParts) {
        if(!back) {
                  $(this).attr('disabled', 'disabled');
       }
}

also works.