html - Jquery Match Value to Array -
i have navigation bar , divs want show when nav bar link clicked on. instead of having divs descendants of nav bar links, separate (i know shouldn't done). so, created data label each nav bar link matches data label of div should shown on click.
so, want click nav link, find data label (which i've done jquery), match data label against different divs , display div (while hiding others). think need create array of divs, have jquery code create array , go through find matching data type, show div...thanks help!
<div id="nav"> <ul> <li><a href="#" class="navselection" data-type="1">1</a></li> <li><a href="#" class="navselection" data-type="2">2</a></li> <li><a href="#" class="navselection" data-type="3">3</a></li> <li><a href="#" class="navselection" data-type="4">4</a></li> <li><a href="#" class="navselection" data-type="5">5</a></li> </ul> </div> <div id="main"> <div id="main1" data-type="1">main 1</div> <div id="main2" data-type="2">main 2</div> <div id="main3" data-type="3">main 3</div> <div id="main4" data-type="4">main 4</div> <div id="main5" data-type="5">main 5</div> </div>
pretty simple, hide divs on click, show 1 want:
$("#nav ul li").click(function() { $("#main div").hide(); //hide others if open var type = $("a", this).data("type"); //get data-type value of clicked li's child tag $("#main div[data-type='" + type + "']").show(); //show matching data-type div });
Comments
Post a Comment