jeudi 13 mai 2021

JQuery this "contains" with if statement

I created a function with the if statement to add a class when nav and breadcrumb text are the same. What is wrong? The if... $(this) doesn't work.... Here is the code:

var $textval = $('.breadcrumb_bread li+li+li+li+li span').text();
$(".nav-child li a").each(function(){ 
     if ($(this).is(":contains('" + $textval +"')")) {
        $(this).parent().addClass('active');
    }
});

Html breadcrumb code:

<ul class="breadcrumb_bread">
    <li class="active">
        <span></span>
    </li>
    <li>
        <a href="#"><span itemprop="name">Home</span></a>
        <span>/</span>
    </li>
    <li>
        <a href="#"><span>Brands</span></a>
        <span>/</span>
    </li>
    <li>
        <a href="#"><span>CCC</span></a>
        <span>/</span>
    </li>
    <li>
        <span>YYY</span>
    </li>
</ul>

Html nav code:

<ul class="nav-child">
    <li>
        <a href="#">XXX</a>
    </li>
    <li>
        <a href="#">YYY</a>
    </li>
    <li>
        <a href="#">ZZZ</a>
    </li>
</ul>

Aucun commentaire:

Enregistrer un commentaire