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
Post a Comment