CLASSIFIED CLASSIFIED - 4 months ago 9
Javascript Question

Cannot read value 'visibility' of undefined - J

I am using jQuery. Whenever the menu.tog function is called, it seems to forget what 'this' (menu) is. I have also tried replacing 'this' with 'menu' in the function.

var usn = $("#userstuff");
var menu = $('#userinfo');

menu.height('0px');

menu.tog=function()
{
if (menu.css('visibility','hidden'))
menu.css('visibility','visible');
else
menu.css('visibility','hidden');
}

Answer

This should work:

// only use 'menu', not '$(menu)' and don't write 'px'
// or in one row: var menu = $('#userinfo').height(0);
var menu = $('#userinfo');
menu.height(0);

// however your `tog` function is called, there may be not referenced
// to 'this', instead use 'menu'
// but keep in mind, 'menu' is a jQuery object here, not a DOM element
menu.tog = function() {
    if ( menu.css("visibility") == "hidden" ) {
        menu.css("visibility", "visible");
    }
    else {
        menu.css("visibility", "hidden");
    }
}

menu.tog();