There is a code:

function submenu(){ var link = $('.menu li'); link.hover(function () { if($(this).find('ul').length > 0){ var submenu = $($(this).find('ul')[0]); open(submenu); } }, function () { close(submenu); //не видит submenu } ); function open(item) { $(item).slideDown().addClass('active'); } function close(item) { $(item).slideUp().removeClass('active'); }} 

How to make visible submenu? If you remove the var and make global - not good.

  • one
    visible where? it is better to come up with a different name for the variable so that it does not coincide with the name of the function - Grundy
  • there is a comment that is visible in the second block of function () {...} - dima_bur
  • 2
    and declare it at the level of this function, in the same place as var link - Grundy
  • Yes, thanks, it helped - dima_bur
  • @Grundy is it just my feeling that it won't work with nested lists? - Igor

1 answer 1

Why do you need this variable?

 function submenu(){ $('.menu li').hover( function () { if($(this).find('ul').length > 0){ open($(this).find('ul')[0]); } }, function () { if($(this).find('ul').length > 0){ close($(this).find('ul')[0]); } } ); function open(item) { $(item).slideDown().addClass('active'); } function close(item) { $(item).slideUp().removeClass('active'); } }