1

this is probably dead simple but I just can find a solution myself. I have this code:

      $('article li.active').each(function() {
        if ($(this).attr('id') == 'arrival-and-departure') {
          $('p.quot').hide();
        }
        else if ($(this).attr('id') == 'additional-needs') {
          $('p.quot').hide();
        }
        else {$('p.quot').show()}
      };

I was wondering how I could combine the two IFs so that I just need IF and Else. Any help would be very welcome, thanks!

1
  • you should use OR in you first if statement if v1 == "test" || v1 =="other"){...} Commented Nov 24, 2016 at 14:48

4 Answers 4

3
$('article li.active').each(function() {
    if ($(this).attr('id') === 'arrival-and-departure' || $(this).attr('id') === 'additional-needs') {
        $('p.quot').hide();
    }
    else {
        $('p.quot').show();
    }
});

The double-pipe operator acts as an "OR" operator. So check if it's "this" OR "that".

Sign up to request clarification or add additional context in comments.

Comments

2

You should use OR:

$('article li.active').each(function() {
    if ($(this).attr('id') == 'arrival-and-departure' || 
        $(this).attr('id') == 'additional-needs') 
    {
      $('p.quot').hide();
    }
    else {$('p.quot').show()}
  };

Comments

2

You can use && (and) and || (or)

var a = true;
var b = false;
var c = true;
if(a&&b) //false since b=false, only a=true
if(a&&c) //true since a&c are both true
if(a||b)//a=true so>will be true. Javascript even won't check B (keep in mind when using functions!

More here: http://www.w3schools.com/js/js_comparisons.asp

Comments

2

You could also use switch statement instead of the if with the || operator, like this:

$('article li.active').each(function() {
    switch ($(this).attr('id')) {
        case 'arrival-and-departure':
        case 'additional-needs':
            $('p.quot').hide();
            break;
        default:
            $('p.quot').show();
    }
});

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.