jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -


my code below changes image people in team sector, can see theres alot of repeated code; there way shorten it?

also currently, allows click on sections without turning off clicked one; assume toggle() function, couldn't work other way! still quite new jquery coding, grand!!

cheers in advance!

$("#team_client_services").click(function () {   if ($(".team_client_services").hasclass("team_hide")) {     $(".team_client_services").toggleclass("team_hide");     $(".team_client_services2").toggleclass("team_show");   } else {     $(".team_client_services").toggleclass("team_hide");     $(".team_client_services2").toggleclass("team_show");   }; });  $("#team_management").click(function () {   if ($(".team_management").hasclass("team_hide")) {     $(".team_management").toggleclass("team_hide");     $(".team_management2").toggleclass("team_show");   } else {     $(".team_management").toggleclass("team_hide");     $(".team_management2").toggleclass("team_show");   }; });  $("#team_products").click(function () {   if ($(".team_products").hasclass("team_hide")) {     $(".team_products").toggleclass("team_hide");     $(".team_products2").toggleclass("team_show");   } else {     $(".team_products").toggleclass("team_hide");     $(".team_products2").toggleclass("team_show");   }; });  $("#team_technical").click(function () {   if ($(".team_technical").hasclass("team_hide")) {     $(".team_technical").toggleclass("team_hide");     $(".team_technical2").toggleclass("team_show");   } else {     $(".team_technical").toggleclass("team_hide");     $(".team_technical2").toggleclass("team_show");   }; });  $("#team_accounts").click(function () {   if ($(".team_accounts").hasclass("team_hide")) {     $(".team_accounts").toggleclass("team_hide");     $(".team_accounts2").toggleclass("team_show");   } else {     $(".team_accounts").toggleclass("team_hide");     $(".team_accounts2").toggleclass("team_show");   }; });  $("#team_admin").click(function () {   if ($(".team_admin").hasclass("team_hide")) {     $(".team_admin").toggleclass("team_hide");     $(".team_admin2").toggleclass("team_show");   } else {     $(".team_admin").toggleclass("team_hide");     $(".team_admin2").toggleclass("team_show");   }; }); 

function doit(name) {     $("#team_"+name).click(function() {        $(".team_"+name).toggleclass("team_hide");        $(".team_"+name+"2").toggleclass("team_show");     }); } 

then call doit 5 times passing "client_services", "management", "products", "technical", "accounts", "admin".


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -