I am trying to optimize the following switch statement, but I'm having a brain fart about how to do it. Here's the code:
$('.newsroom-tabs div.pane-content ul li').click(function(){ // get the class name of the clicked link var className = $(this).attr('class'); // variable so that jQuery isn't traversing the DOM each time var hideFeaturedArticle = $('.featured-article').hide(); // based on the category selected, the featured article will // show for that section switch(className) { case 'air-quality-news-tab': hideFeaturedArticle; $('.featured-article.aqn').fadeIn(300); break; case 'awards-and-reviews-tab': hideFeaturedArticle; $('.featured-article.aar').fadeIn(300); break; case 'in-the-community-tab': hideFeaturedArticle; $('.featured-article.itc').fadeIn(300); break; case 'press-releases-tab': hideFeaturedArticle; $('.featured-article.pr').fadeIn(300); break; default: return className; } });
I feel like hiding the tabs each time is inefficient, but I'm not sure how to do this otherwise. Is there anything else I could do better here?
hideFeaturedArticle;
does nothing at all, in each of yourcase
s.\$\endgroup\$