user3590304
user3590304

Reputation: 69

Hiding Div by passing var to function

I have a nav menu and based on which item the user clicks i would like to show them a div, now my problem is, when they click a new menu the current div needs to be hidden again but i cant see how to pass the var through?

$(document).ready(function () {
    $('.submenu > li > a').click(function () {
        $('#' + lastClicked).hide(); // hides previous div
        var menu = $(this).prop('id') + "_view";
        $('#news').hide();
        $('#' + menu).toggleClass("hidden");
        var lastClicked = menu;
    });
});

I realize its because i'm not passing lastClicked back through, but how would i do this?

also when using toggleClass, i found the only was to pass in the property id is by doing '#'+menu, is this correct?

Upvotes: 0

Views: 49

Answers (1)

caspian
caspian

Reputation: 1804

define lastClicked in a more global scope...

$(document).ready(function(){        
    var lastClicked = ''; 

    $('.submenu > li > a').click(function(){
        $('#'+lastClicked).hide(); // hides previous div
        var menu = $(this).prop('id') +"_view";

        $('#news').hide();
        $('#'+menu).toggleClass("hidden");
        lastClicked = menu;
    });
});

Upvotes: 1

Related Questions