• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Don't work after reload (script js)

Allan

Well-known member
#1
Hey, i test this script:

Code:
<!--
$('.categoryNodeInfo').prepend("<div class='showhideNodes hideNodes'>{xen:phrase showhideNodes_collapse}</div>");
var showhideNodesCookie = $.getCookie('showhideNodes');
var showhideNodesArray=new Array();
if (showhideNodesCookie) {
showhideNodesArray = showhideNodesCookie.split(",");
$.each(showhideNodesArray,function(){
        var selector = 'li[id='+this+']';
        $(selector+' ol.nodeList').hide();
        $(selector+' .showhideNodes').toggleClass('showNodes');
        $(selector+' .showhideNodes').toggleClass('hideNodes');
        $(selector+' .showhideNodes').text('{xen:phrase showhideNodes_expand}');
});
}
$('.showhideNodes').click(
function() {
if ($(this).text() == '{xen:phrase showhideNodes_collapse}') {
    $(this).parent().next().slideUp();
    $(this).text('{xen:phrase showhideNodes_expand}');
    //if($.inArray(this,showhideNodesArray) != -1){
        showhideNodesArray.push($(this).parent().parent().attr('id'));
        $.setCookie('showhideNodes',showhideNodesArray.join(","));
    //}
}
else if ($(this).text() == '{xen:phrase showhideNodes_expand}') {
    $(this).parent().next().slideDown();
    $(this).text('{xen:phrase showhideNodes_collapse}');
        var remove = $(this).parent().parent().attr('id');
        showhideNodesArray.splice($.inArray(remove,showhideNodesArray),1);
        $.setCookie('showhideNodes',showhideNodesArray.join(","));
}
$(this).toggleClass('showNodes');
$(this).toggleClass('hideNodes');
});
//-->
It works once and after reloading the page doesn't work.

I have this error in console:

Screenshot_1.png

What about you?
 

Chris D

XenForo developer
Staff member
#2
This line is wrong:

Code:
var selector = 'li[id='+this+']';
The correct way to specfy an element with an ID would be:

Code:
var selector = 'li#' + this;
 

Allan

Well-known member
#3
This line is wrong:

Code:
var selector = 'li[id='+this+']';
The correct way to specfy an element with an ID would be:

Code:
var selector = 'li#' + this;
The reload work now ! Thank you Chris :)

But don't keeps the user preferences (cookies) :(