How to create subcategories with Javascript

2

I have this list:

<li>
    <a href="" class="category-subtree-expandable">Vestuário</a>
    <ul>
        <li>
            <a href="" class="category-subtree-expandable">Feminino</a>
            <ul>
                <li>
                    <a href="/boa-forma/zumba-fitness/vestuario/feminino/blusas/camisetas" title="Camisetas">Camisetas</a>
                </li>
                <li>
                    <a href="" title="Masculino">Masculino</a>
                </li>
                <li>
                    <a href="" title="Regatas">Regatas</a>
                </li>
                <li>
                    <a href="" title="Tops">Tops</a>
                </li>
                <li>
                    <a href="" title="Capri">Capri</a>
                </li>
                <li>
                    <a href="" title="Legging">Legging</a>
                </li>
            </ul>
        </li>
        <li>
            <a href="">Masculino</a>
        </li>
        <li>
            <a href="">Unissex</a>
        </li>
    </ul>
</li>

And I have this script:

if ($submenu) {
    [].forEach.call($submenu.querySelectorAll('.category-subtree-expandable'), function ($el) {
        $el.href = '#';
        $el.addEventListener('click', function (event) {
            event.preventDefault();
            this.nextElementSibling.classList.toggle('active');
            this.classList.toggle('active');
        });
    });
}

When I'm on a page in a subcategory, the precise subcategory is highlighted, eg change the color to subcategory

    
asked by anonymous 30.07.2015 / 03:08

1 answer

1

How about using CSS to select the element through the Category Title to put the asset class? for example,

// Javascript nativo
document.querySelector('a[title="Masculino"]').classList.add('active');

// ou em jQuery
$('a[title="Masculino"]').addClass('activo');
    
30.07.2015 / 15:44