First of all; I'm sorry if my quesiton is irrelevant, because i'm newbie on jQuery
I have a menu in my website. Here is HTML;
<ul class="page-sidebar-menu">
<li data-link="index.php"><a href="index.php">Home</a></li>
<li data-link="logout.php"><a href="logout.php">Logout</a></li>
</ul>
I'm taking url path to compare with li data-link to add active class to li
But unfortunately it's returning undefined.
Here is my javascript / jQuery;
var pathname = window.location.pathname; // Returns path only
var encPath = encodeURIComponent(pathname); // Encoded URL
var splPath = encPath.split('%2F');
var cleanUrl = splPath.pop(); // Clean Url
var dataUrl = $('ul.page-sidebar-menu > li').attr('data-link');
$(dataUrl).each(function( i ) {
if(dataUrl === cleanUrl) {
dataUrl.addClass('active');
}
});
So what's wrong with my code? How can i fix it? Any help greatly appricated.
PS: Sorry for bad English.