Apologies of this has been covered before but I couldn't find anything specific...
I'm new to JQuery and have created some tabbed content boxes that work well, however, I know the code used isn't as efficient as it should be!
Is there a way I can optimise the code below so that I don't have to specify each tabbed button and each tabbed content?
Thanks in advance for any help! Code below :)
HTML
<div id="tabs"> <div id="tabbed-buttons"> <div id="tb" class="tb1"> Plumbing </div> <div id="tb" class="tb2"> Heating </div> <div id="tb" class="tb3"> Underfloor Heating </div> <div id="tb" class="tb4"> Renewable Energy </div> </div> <div id="tabbed-content"> <div id="tc1"> Text for tabbed content 1 </div> <div id="tc2"> Text for tabbed content 2 </div> <div id="tc3"> Text for tabbed content 3 </div> <div id="tc4"> Text for tabbed content 4 </div> </div> </div> JQUERY
// ---------- ---------- Tabbed Content ---------- ---------- $(document).ready(function(){ $("#tc2, #tc3, #tc4").hide(); $(".tb1").css({ background: "#4867ad", background: "-moz-linear-gradient(top, #273264 0%, #4867ad 100%)", background: "-webkit-gradient(linear, left top, left bottom, color-stop(0%,#273264), color-stop(100%,#4867ad))", background: "-webkit-linear-gradient(top, #273264 0%,#4867ad 100%)", background: "-o-linear-gradient(top, #273264 0%,#4867ad 100%)", background: "-ms-linear-gradient(top, #273264 0%,#4867ad 100%)", background: "linear-gradient(to bottom, #273264 0%,#4867ad 100%)", color: "#fff" }); $(".tb1").click(function() { $("#tc1").show(); $("#tc2, #tc3, #tc4").hide(); $(".tb1").css({ background: "#4867ad", background: "-moz-linear-gradient(top, #273264 0%, #4867ad 100%)", background: "-webkit-gradient(linear, left top, left bottom, color-stop(0%,#273264), color-stop(100%,#4867ad))", background: "-webkit-linear-gradient(top, #273264 0%,#4867ad 100%)", background: "-o-linear-gradient(top, #273264 0%,#4867ad 100%)", background: "-ms-linear-gradient(top, #273264 0%,#4867ad 100%)", background: "linear-gradient(to bottom, #273264 0%,#4867ad 100%)", color: "#fff" }); $(".tb2, .tb3, .tc4").css({background:"auto",color:"#444"}); }); $(".tb2").click(function() { $("#tc1, #tc3, ¢tc4").hide(); $("#tc2").show(); $(".tb2").css({ background: "#4867ad", background: "-moz-linear-gradient(top, #273264 0%, #4867ad 100%)", background: "-webkit-gradient(linear, left top, left bottom, color-stop(0%,#273264), color-stop(100%,#4867ad))", background: "-webkit-linear-gradient(top, #273264 0%,#4867ad 100%)", background: "-o-linear-gradient(top, #273264 0%,#4867ad 100%)", background: "-ms-linear-gradient(top, #273264 0%,#4867ad 100%)", background: "linear-gradient(to bottom, #273264 0%,#4867ad 100%)", color: "#fff" }); $(".tb1, .tb3, .tc4").css({background:"auto",color:"#444"}); }); $(".tb3").click(function() { $("#tc1, #tc2, #tc4").hide(); $("#tc3").show(); $(".tb3").css({ background: "#4867ad", background: "-moz-linear-gradient(top, #273264 0%, #4867ad 100%)", background: "-webkit-gradient(linear, left top, left bottom, color-stop(0%,#273264), color-stop(100%,#4867ad))", background: "-webkit-linear-gradient(top, #273264 0%,#4867ad 100%)", background: "-o-linear-gradient(top, #273264 0%,#4867ad 100%)", background: "-ms-linear-gradient(top, #273264 0%,#4867ad 100%)", background: "linear-gradient(to bottom, #273264 0%,#4867ad 100%)", color: "#fff" }); $(".tb1, .tb2, .tb4").css({background:"auto",color:"#444"}); }); $(".tb4").click(function() { $("#tc1, #tc2, #tc3").hide(); $("#tc4").show(); $(".tb4").css({ background: "#4867ad", background: "-moz-linear-gradient(top, #273264 0%, #4867ad 100%)", background: "-webkit-gradient(linear, left top, left bottom, color-stop(0%,#273264), color-stop(100%,#4867ad))", background: "-webkit-linear-gradient(top, #273264 0%,#4867ad 100%)", background: "-o-linear-gradient(top, #273264 0%,#4867ad 100%)", background: "-ms-linear-gradient(top, #273264 0%,#4867ad 100%)", background: "linear-gradient(to bottom, #273264 0%,#4867ad 100%)", color: "#fff" }); $(".tb1, .tb2, .tb3").css({background:"auto",color:"#444"}); }); });