ether ether - 1 month ago 10
jQuery Question

Multiple if statements with multiple conditions?

I'm trying to check my page's

<title>
tag for certain key words, to show/hide a div. Each state (4 states) have 4 differrent keywords they could have in the title. I'm trying to figure out how I can write my if statement so it says


If title has "word 1" or "word 2" or "word 3" or "word 4" do this function


Currently, I have it written like this:

if ($("title").text() == "Marketing" || "Marketing Product Support" || "Inbound Marketing Best Practices" || "Marketing Certification Help") {
$('.marketing-board-header').show();
}

if ($("title").text() == "Sales" || "Sales Product Support" || "Inbound Sales Best Practices" || "Sales Certification Help") {
$('.sales-board-header').show();
}

if ($("title").text() == "COS Design" || "COS Design Support" || "Share Your Work" || "Design Certification Help") {
$('.design-board-header').show();
}

if ($("title").text() == "community Ideas") {
$('.idea-board-header').show();
}


But this doesn't seem to hide/show the divs I'd like, am I missing something? or is there a better way to do this?

Thanks for the help!

Answer

I would probably use a switch statement instead.

jQuery(document).ready(function(){
    switch ( jQuery('title').text() )
    {
       case "Marketing":
       case "Marketing Product Support":
       case "Inbound Marketing Best Practices": 
           jQuery('.marketing-board-header').show();
           break;
       case "Sales":
       case "Sales Product Support":
       case "Inbound Sales Best Practices": 
           jQuery('.sales-board-header').show();
           break;
    }
});

etc...