0

In the below script I added or operator to check the condition, Like if it is "ALL TCP" or "ALL UDP" item must get disabled, Please suggest me to modify the code.

<script type="text/javascript">
    function chgJob(pThis, pRow)
    {
        if ((pThis.value != 'All TCP') || (pThis.value != 'All UDP'))
        {
            $x_disableItem('f02_' + pRow, false);
        }
        else
        {
            $x_disableItem('f02_' + pRow, true);
        }
    }
</script>
3
  • if ((pThis.value != 'All TCP') || (pThis.value != 'All UDP')) can also be written as if ((pThis.value == 'All TCP') && (pThis.value == 'All UDP')) Commented Dec 4, 2012 at 7:36
  • 1
    @SimonLaing: Incorrect. Your and statement will always return false, since pThis.value can't possibly be 'All TCP' and 'All UDP' a the same time. Commented Dec 4, 2012 at 7:47
  • @Cerbrus, you are right too, misread the || Commented Dec 4, 2012 at 10:29

3 Answers 3

4

A string can never be equal to 'All TCP' and equal to 'All UDP' at the same time, so the condition will always be true.

You want to use the && operator instead of the || operator:

if ((pThis.value != 'All TCP') && (pThis.value != 'All UDP'))

Edit:

As Cerbrus pointed out, you don't need the if statement at all, you can use the boolean expression directly by inverting it (x==y || x==z is the same as !(x!=y && x!=z)):

<script type="text/javascript">
function chgJob(pThis, pRow) {
    $x_disableItem('f02_' + pRow, pThis.value == 'All TCP' || pThis.value == 'All UDP');
}
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Since you've got the most votes already, would you want to add my suggestion about omitting the if-else altogether?
0

try this:

if ((pThis.value != 'All TCP') && (pThis.value != 'All UDP'))

or :

function chgJob(pThis, pRow)
{
    if ((pThis.value == 'All TCP') || (pThis.value == 'All UDP'))
    {
        $x_disableItem('f02_' + pRow, true);
    }
    else
    {
        $x_disableItem('f02_' + pRow, false);
    }
}

Comments

0

Like has been said already, in your if-else, you'll need the && operator, instead of the ||, but:

You can also remove the if altogether, by doing this instead:

function chgJob(pThis, pRow) {
    $x_disableItem('f02_' + pRow, (pThis.value == 'All TCP' || pThis.value == 'All UDP'));
}

If pThis.value is "ALL TCP" or "ALL UDP", the item will be disabled.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.