get id of closest ‘ul’ or parent id and get value of ‘href’ attribute with Jquery

html:

//if menu is created dynamically it is very useful
<ul id="slider" style="display: none;">
 <li class="panel cloned">test1</li>
 <li class="panel">
  <article>
   <h1><a href="/Admin/RoleMaster"> Role Creation </a> </h1>
  </article>
 </li>
 <li class="panel cloned">test2</li>
</ul>

js:

var url = window.location.pathname; //or window.location.toString();

$('#slider li.panel h1 a').each(function(){
   var myHref= $(this).attr('href');
   if( url == myHref) //compare current url with url value
  {

        var ulId = $(this).closest('ul').attr('id'); //get id of ul tag 
        $("#"+ulId).css("display", "block");
   }
});
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s