How to capture the href attribute of an internal link in jQuery and use the value of # for selecting a div tag Id -
i have navigation contains links such as:
<a href="#div1">div 1</a> <a href="#div2">div 2</a> <a href="#div3">div 3</a>
also have 3 div tags follow
<div id="div1"> content </div> <div id="div2"> content </div> <div id="div3"> content </div>
i want when click on
<a href="#div1">div 1</a>
then shows content of
<div id="div1"> content </div>
and hide other div tags , on..
this should work:
$('a').click(function(e) { var d = this.hash; // contains hash portion of clicked url $('div').not(d).hide(); // hide every other div $(d).show(); // show 1 e.preventdefault(); // prevent page reload });
that said, ought have other class attached divs supposed hidden or shown, ensure can use divs within rest of markup.
working demo @ http://jsfiddle.net/alnitak/jz7rj/
Comments
Post a Comment