2

How can I refer $('nav > .smart-nav > li') with $(this) inside if statement, instead of $('.nav-second')?

This is the code-

if ($('nav > .smart-nav > li').is('.active')) {
  $('.nav-second').addClass('in');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
  <ul class="nav smart-nav">
    <li><a href=""><i class="fa fa-home"></i> <span>Dashboard</span></a></li>
    <li class="active">
      <a data-toggle="collapse" aria-expanded="false" class="collapsed">
        <i class="fa fa-home"></i><span>Administrator Portal</span><span class="sub-nav-icon"> <i class="fa fa-angle-down"></i> </span>
      </a>
      <ul id="" class="nav nav-second collapse" aria-expanded="false">
        <li><a href="administrator-portal.html">Dashboard</a></li>
        <li><a href="activity.html">Customer Report</a></li>
      </ul>
    </li>
    <li>
      <a data-toggle="collapse" aria-expanded="false" class="collapsed">
        <i class="fa fa-support"></i><span>Support</span><span class="sub-nav-icon"> <i class="fa fa-angle-down"></i> </span>
      </a>
      <ul id="" class="nav nav-second collapse" aria-expanded="false">
        <li><a href="tableStyles.html">Logs</a></li>
      </ul>
    </li>
  </ul>
</nav>
Serving Quarantine period
  • 66,345
  • 10
  • 43
  • 85
LXhelili
  • 895
  • 5
  • 13

4 Answers4

2

$('nav > .smart-nav > li').is('.active') is checking if any of those li that has the class active, instead if you want the li with the active class, I would do something like this:

var li = $('nav > .smart-nav > li.active'); // get the active li

if(li.length){  // check if there are any
  li.find('.nav-second').addClass('in'); // here the li var acts like your this as it is the ones with the active class
}
Pete
  • 51,385
  • 23
  • 99
  • 140
2

Do like below:-

$('.smart-nav > li.active').children('ul.nav-second').addClass('in');

Example:-

$('.smart-nav > li.active').children('ul.nav-second').addClass('in');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
    <ul class="nav smart-nav">
        <li><a href=""><i class="fa fa-home"></i> <span>Dashboard</span></a></li>
        <li class="active">
            <a data-toggle="collapse" aria-expanded="false" class="collapsed">
                <i class="fa fa-home"></i><span>Administrator Portal</span><span class="sub-nav-icon"> <i class="fa fa-angle-down"></i> </span>
            </a>
            <ul id="" class="nav nav-second collapse" aria-expanded="false">
                <li><a href="administrator-portal.html">Dashboard</a></li>
                <li><a href="activity.html">Customer Report</a></li>
            </ul>
        </li>
        <li>
            <a data-toggle="collapse" aria-expanded="false" class="collapsed">
                <i class="fa fa-support"></i><span>Support</span><span class="sub-nav-icon"> <i class="fa fa-angle-down"></i> </span>
            </a>
            <ul id="" class="nav nav-second collapse" aria-expanded="false">
                <li><a href="tableStyles.html">Logs</a></li>
            </ul>
        </li>
    </ul>
</nav>
Serving Quarantine period
  • 66,345
  • 10
  • 43
  • 85
  • @Satpal changed based on html shown by OP right now – Serving Quarantine period Jun 09 '17 at 11:43
  • 1
    This would be the best way to add a class - but not sure if there is any other code meant to go inside the `if` (which was why I left it in my answer). Plus one anyway :) – Pete Jun 09 '17 at 11:52
  • Thank you, it works now, I have two options to use now. I marked another answer as correct before. I just can give a thumbs up for this one! – LXhelili Jun 09 '17 at 11:56
  • 1
    @LXhelili mark this one as the answer - it would be the better way to do it if you don't need the `if` to do other stuff – Pete Jun 09 '17 at 11:58
1

I believe you can't. $(this) is available only in events on elements and callbacks

SpeekaDievs
  • 193
  • 1
  • 11
1

You don't need an if() at all if you are looking for a descendant of the active <li> ...just combine selectors:

$('nav > .smart-nav > li.active .nav-second').addClass('in');
charlietfl
  • 164,229
  • 13
  • 110
  • 143