javascript - Add class to another page attribute using URL id - Jquery -
i have attached id url. ex: when click link 1
, has #tab-2
id , takes me landing page. in landing page have find element has #tab-2
id have add class .active
.
i tried no luck. please 1 correct code. thanks
html
<div class="menu"> <ul> <li><a href="http://jsfiddle.net/gscyoa0j/1/#tab-2">link 1</a></li> <li><a href="http://jsfiddle.net/gscyoa0j/1/#tab-44">link 2</a></li> <li><a href="http://jsfiddle.net/gscyoa0j/1/#tab-74">link 3</a></li> </ul> </div>
landing page
<div class="tab"> <ul> <li><a id="tab-2" class="active" >link 1 content</a></li> <li><a id="tab-44" >link 2 content</a></li> <li><a id="tab-74" >link 3 content</a></li> </ul> </div>
jquery
$(document).ready(function(){ $(".menu ul li a").each(function(){ var url = window.location.pathname; var id = url.substring(url.lastindexof('/') + 1); var land = $('.tab').find("id"); if( land == id ) { $('this').addclass('active'); } }) });
assuming i've understood requirement, can retrieve hash value url of page using window.location.hash
. can use directly select required element , add class on load of new page:
$(function() { var id = window.location.hash; // = '#tab-2', in first link. $(id).addclass('active'); });
Comments
Post a Comment