0

trying to narrow down <a> hrefs to one with jQuery filter and then add the tag active class.
I am doing this because I get redirected from sidebar submenu and want to still keep sidebar menu open.

$(document).ready(function () {
  var url = window.location;


  $('ul.treeview-menu a').filter(function () {
      return url.indexOf(this.href) > -1;
  }).parentsUntil(".sidebar-menu > .treeview-menu").addClass('active');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="sidebar-menu" data-widget="tree">
    <li class="header">General</li>
    <li class="home"><a href="/p/index.php"><i class="fas fa-tachometer-alt"></i> <span> Dashboard</span></a></li>

    <li class="header">Data Managment</li>
    <li class="treeview">
        <a href="#">p <span>Tree</span>
            <span class="pull-right-container">
                <i class="fa fa-angle-left pull-right"></i>
            </span>
        </a>
        <ul class="treeview-menu">
            <li><a href="/p/pages/foo.php">foo</a></li>
            <li><a href="/p/pages/bar.php">bar</a></li>
            <li><a href="/p/pages/blah.php">blah</a></li>
            <li><a href="/p/pages/blahh.php">blahh</a></li>
        </ul>
    </li>
    
    <li class="treeview">
        <a href="#"></i> <span>Tree 2</span>
            <span class="pull-right-container">
                <i class="fa fa-angle-left pull-right"></i>
            </span>
        </a>
        <ul class="treeview-menu">
            <li><a href="/p/pages/aaa.php">aaa</a></li>
            ...
        </ul>
    </li>

</ul>

then if I get redirected inside foo and href be like /p/pages/foo.php/new.php I still want to foo be active.

why don't this work?

Ali Esmailpor
  • 1,209
  • 3
  • 11
  • 22
Ramiel
  • 33
  • 1
  • 8
  • without looking too closely (and without relevant html), this bit `.parentsUntil(".sidebar-menu > .treeview-menu").addClass('active')` *looks like* it should be `.closest(".sidebar-menu").find(".treeview-menu").addClass('active')` – freedomn-m Feb 07 '21 at 10:59
  • 1
    @freedomn-m unfortunately didn't work. i'll try to share some html – Ramiel Feb 07 '21 at 11:02
  • You should compare `window.location` with `this.href` (by "compare" I mean console.log them and see what you're actually getting. It's hard to tell if the issue is the .filter or the .closest. Also "didn't work" and "why doesn't this work" don't really help - do they do *something*? *anything*? How many results do you get from your `.filter`? – freedomn-m Feb 07 '21 at 11:15
  • Assuming your .filter gives one result, from your html, your parentsUntil / find should highlight all of the .treeview-menu's - again, assuming your .filter works... it would be `.filter(..).closest(".treeview-menu").addClass("active")` – freedomn-m Feb 07 '21 at 11:16
  • 1
    @freedomn-m i get this error in console `url.indexOf is not a function` and `.closest(".sidebar-menu").find(".treeview-menu").addClass('active')` returns all of them but html is not right – Ramiel Feb 07 '21 at 11:27
  • 1
    @freedomn-m console error is gone by replacing `url` with `window.location.href` – Ramiel Feb 07 '21 at 11:30
  • 1
    @freedomn-m `return console.log(window.location.href.indexOf(this.href) > -1)` returns `false`. it means it can't find anything!? – Ramiel Feb 07 '21 at 11:33
  • What do you get with `console.log(window.location.href, this.href)` on each of the `a` (inside the .filter)? – freedomn-m Feb 07 '21 at 11:57
  • 1
    @freedomn-m nothing. it's empty – Ramiel Feb 07 '21 at 14:03
  • Your window.location.href is empty? Not going to be able to match on it then... – freedomn-m Feb 07 '21 at 16:16

1 Answers1

1

Other way would be using each loop and then comparing if the url == this.href if yes then add class to that li and open closest ul.

Demo Code :

$(document).ready(function() {
  //just for demo...
  var url = 'https://stacksnippets.net/p/pages/bar.php';
  $('ul.treeview-menu a').each(function() {
    //if full url is not same with li use indexof else if http://...also there in href of a tag use below..
    if (url == this.href) {
      $(this).addClass('active') //add class active to `a`
      $(this).closest("ul").slideDown(); //slide ul tag where `li` is active
    }
  })
})
.treeview-menu {
  display: none
}

.active {
  color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="sidebar-menu" data-widget="tree">
  <li class="header">General</li>
  <li class="home"><a href="/p/index.php"><i class="fas fa-tachometer-alt"></i> <span> Dashboard</span></a></li>

  <li class="header">Data Managment</li>
  <li class="treeview">
    <a href="#">p <span>Tree</span>
            <span class="pull-right-container">
                <i class="fa fa-angle-left pull-right"></i>
            </span>
        </a>
    <ul class="treeview-menu">
      <li><a href="https://stacksnippets.net/p/pages/foo.php">foo</a></li>
      <li><a href="https://stacksnippets.net/p/pages/bar.php">bar</a></li>
      <li><a href="https://stacksnippets.net/p/pages/blah.php">blah</a></li>
      <li><a href="https://stacksnippets.net/p/pages/blahh.php">blahh</a></li>
    </ul>
  </li>

  <li class="treeview">
    <a href="#"> <span>Tree 2</span>
      <span class="pull-right-container">
                <i class="fa fa-angle-left pull-right"></i>
            </span>
    </a>
    <ul class="treeview-menu">
      <li><a href="https://stacksnippets.net/p/pages/aaa.php">aaa</a></li>
      ...
    </ul>
  </li>

</ul>

Updated Code :

$(window).on('load', function() {
  var url = window.location.href;
  var new_url = url.slice(url.lastIndexOf('/') + 1);
  $('ul.treeview-menu a').each(function() {
    var res = this.href.replace(".php", "");
    var href = res + "/" + new_url;
    if (href == url) {
      $(this).parent().addClass('active') //add class active to `a
      $(this).closest("ul").slideDown(); //slide ul tag where `li` is active
    }
  })
});
Swati
  • 28,069
  • 4
  • 21
  • 41
  • 1
    the urls are not the same and i have been redirected from submenu, and the new url has a new part at the end now how would you use `indexOf`? – Ramiel Feb 07 '21 at 14:13
  • Hi like [this](https://jsfiddle.net/935d4rs7/) check if that works for you . – Swati Feb 07 '21 at 14:24
  • 1
    Hi :) i can't tell, i couldn't get rid of this error `jQuery.Deferred exception: url.lastIndexOf is not a function TypeError: url.lastIndexOf is not a function` and `Uncaught TypeError: url.lastIndexOf is not a function` sorry for my beginner questions – Ramiel Feb 07 '21 at 14:38
  • 1
    check [this](https://stackoverflow.com/questions/37738732/jquery-3-0-url-indexof-error) post try that suggestion see if that works or not :) – Swati Feb 07 '21 at 14:45
  • 1
    ok i did kind of solve the error. and `console.log(new_url)` shows urls last part like this `new.php` and if statement returns empty – Ramiel Feb 07 '21 at 16:58
  • rn the `new_url` returning `new.php` and each loop and `this.href` is showing the sidebar urls + returning new url multiple time with a number at the left side witch is the length of each treeview – Ramiel Feb 08 '21 at 12:54
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/228415/discussion-between-swati-and-ramiel). – Swati Feb 08 '21 at 13:00