html - Connecting link item to content div id with jQuery -


i'm attempting create simple, multi-tabbed navigation utilizes single web page. have content laid out so:

html

<ul id="nav">     <li><a href="tab1">tab 1</a></li>     <li><a href="tab2">tab 2</a></li>     <li class="active"><a href="tab3">tab 3</a></li> </ul><!--/#nav-->  <div id="content-box">     <div id="tab1">         <!--content tab1-->     </div><!--/#tab1-->      <div id="tab2">         <!--content tab2-->     </div><!--/#tab2-->      <div id="tab3">         <!--content tab3-->     </div><!--/#tab3-->  </div><!--/#content-box--> 

jquery:

$("#nav li").on("click", function(event){         $("#nav li").removeclass("active");         $(this).addclass("active");         $("#content-box").children().hide();     }); 

my problem don't know how link li class active corresponding div. of tab divs set hidden, , have class called .activeslide turns sets visibility: visible.

use html 5 data attributes link div

html

<ul id="nav">   <li data-content="tab1">tab 1</li>   <li data-content="tab2">tab 2</li>   <li data-content="tab3" class="active">tab 3</li> </ul><!--/#nav--> 

jquery

$("#nav li").on("click", function(event){     $("#nav li").removeclass("active");     $(this).addclass("active");     $("#content-box").children().hide();     $('#'+ $(this).data('content')).show();      //or     $("#content-box").children().removeclass('activeslide');     $('#'+ $(this).data('content')).addclass('activeslide'); }); 

fiddle here

updated

add in ready function

 $(document).ready(){      $("#content-box").children().hide();     $("#nav li").on("click", function(event){       ...... //above function goes here     });  }); 

updated fiddle

however recommned use jquery ui tab instead of reinventing wheel... :)


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -