user6578454
user6578454

Reputation:

Trigger jquery when specific list item has active class with specific href inside list item

I want to do a jQuery check when the third tab gets active by getting the class acrive. But I can't get the jQuery to trigger.

This is a small sample html;

<ul>
  <li><a href="#tab-1">Tab1</a></li>
  <li><a href="#tab-2">Tab2</a></li>
  <li class="active"><a href="#tab-3">Tab3</a></li>
</ul>

Only when <a href="#tab-3">'s parent li get class active something should happen, but I can't get it to work;

This what I tried with jQuery:

jQuery( document ).ready(function() {
    var onTabThree = jQuery("ul li.active>a[href='#tab-3']");

    if (onTabThree) {
        do_check();
    }
});

How can I achieve this?

Upvotes: 0

Views: 101

Answers (1)

Pankaj Makwana
Pankaj Makwana

Reputation: 3050

Try this working example. in if condition just add onTabThree.length.

jQuery( document ).ready(function() {
    var onTabThree = jQuery("ul li.active > a[href='#tab-3']");

    if (onTabThree.length) {
        alert("is it working");
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<ul>
  <li><a href="#tab-1">Tab1</a></li>
  <li><a href="#tab-2">Tab2</a></li>
  <li class="active"><a href="#tab-3">Tab3</a></li>
</ul>

Upvotes: 1

Related Questions