javascript - Need help writing a better if statement with JS/jQuery -


is there better way of writing this? seems if statements combined somehow.

here html

<header class="gray_bg" id="main_header"> 

i wrote code change class name of header tag. works fine, feel shorter. great still learning js/jquery. thanks!

$("nav#main_nav li a").on('click', function(){     var link = $("nav#main_nav li a").index(this);     var banner = $("header#main_header");      if (link === 0) {         banner.attr('class', "gray_bg");     }      if (link === 1) {         banner.attr('class', "orange_bg");     }      if (link === 2) {         banner.attr('class', "green_bg");     }      if (link === 3) {         banner.attr('class', "purple_bg");     } }); 

$("nav#main_nav li a").on('click', function(){     var link = $("nav#main_nav li a").index(this);     var colors = ["gray_bg", "orange_bg", "green_bg", "purple_bg"];      $("header#main_header").attr('class', colors[link]); }); 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -