How to activate a li depending on the page where I am with JQUERY?

0

I have the following JS code:

var theme_list_open = false;

    var aurl = window.location.href; // Get the absolute url
    $('li a').filter(function() { 
        $(this).prop('href') === aurl;
        if (theme_list_open == true) {
            $("ul li ul").hide();
            theme_list_open = false
        } else {
            $("ul li ul").show();
            theme_list_open = true
        }
        return
    }).parent('li').addClass('active');

But I do not know what is happening, because when I enter a link what it does is that all child-list activates them, that is:

Usuario (link Principal)
ingresar usuario (link hija)
actualizar usuario (link hija)
borrar usuario (link hija)

Departamento (link Principal)
ingresar dpto (link hija)
actualizar dpto (link hija)
borrar dpto (link hija)

It happens that doing click in any link will immediately display all the links that depend on a link padre ...

I hope you have made me understand and I would appreciate the collaboration.

    
asked by JDavid 03.04.2017 в 22:11
source

2 answers

1

it's easier to do it by css

.current{
   background-color: #000;
}

and to which you need to make the change you put the class either by jquery or by the back you have.

    
answered by 03.04.2017 в 23:17
0

Try the following code:

var theme_list_open = false;
var aurl = window.location.href; // Get the absolute url
$('li a').filter(function() { 
    if ($(this).prop('href') === aurl) {
     if (theme_list_open == true) {
         $(this).find("ul li ul").hide();
         theme_list_open = false
     } else {
         $(this).find("ul li ul").show();
         theme_list_open = true
     }
    }
    return
}).parent('li').addClass('active');
    
answered by 03.04.2017 в 23:06