0

I am trying to build an array of objects to save a menu's state in a cookie but it is not updating because it is not finding the proper index containing the object I need to update visibility for. What am I doing wrong and/or can you point me in the right direction?

$(document).ready(function() {
    var menu = new Array();
    $("#vertical-nav .head").click(function() {
        var c = $(this).next().attr('name');
        $(this).next().slideToggle("slow", function() {
            if ( $(this).next().is(':visible') ) {
                menu.push({
                    name: c,
                    visible: true,
                });
            } else {
                var index = $.inArray(c, menu);
                console.log("INDEX: " + index);
                menu[index] = { name: c, visible:false };
            }
        });
        console.log(menu);
    });
});

This is what I ended up with:

$(document).ready(function() {
    var menu = new Array();
    if ( $.cookie('menu') ) {
        var items = JSON.parse($.cookie('menu'));
        for ( var item in items ) {
            if ( items[item].visible ) {
                $("div[name='" + items[item].name + "']").show();
            }
            menu.push(items[item]);
        }
    }

    $("#vertical-nav .head").click(function() {
        var c = $(this).next().attr('name');
        if ( $(this).next().is(":visible") ) {
            hide(this, c);
        } else {
            show(this, c);
        }
    });

    function show(obj, c) {
        $(obj).next().slideDown('slow', function() {
            var elementExists = false;
            if ( menu.length > 0 ) {
                for ( var item in menu ) {
                    if ( menu[item].name == c ) {
                        elementExists = true;
                        menu[item].visible = true;
                    }
                }
            }
            if ( !elementExists ) {
                menu.push({
                    name: c,
                    visible: true,
                });
            }
            $.cookie('menu', JSON.stringify(menu));
        });
    }

    function hide(obj, c) {
        $(obj).next().slideUp('slow', function() {
            if ( menu.length > 0 ) {
                for ( var item in menu ) {
                    if ( menu[item].name == c ) {
                        menu[item].visible = false;
                    }
                }
            }
            $.cookie('menu', JSON.stringify(menu));
        });
    }
});
2
  • I dont have time to write up an example at the moment, but check out jQuery's grep function, it most likely contains what you are looking for: api.jquery.com/jQuery.grep Commented Apr 18, 2014 at 18:09
  • 1
    console.log(menu) at the end is happening before your if statement that modifies menu. Stick it inside a setTimeout with a 5 second delay and you'll see what i mean. Commented Apr 18, 2014 at 18:14

1 Answer 1

1

You could easily do this:

if (menu.length > 0) {
    for (var item in menu) {
        if (menu[item].name=="some text") alert("Found it!");
    }
}

What I would consider is instead of [{name: "item"; visible: true}] is switch to a key/value pair. You can then use {"item": true}, but this is limited to only storing the visible value and no extra info can be stored in the key. To solve this, try {"item": {visible: true}}. Here's an example of detection in JS using this approach:

if (menu.length > 0) {
    for (var item in menu) {
        var strVisible="hidden";
        if (menu[item].visible==true) strVisible="visible";
        alert(item+" is "+strVisible);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.