3
  $('.sub-menu').on('click', function(){
    var menu = $(this).parents('.row').next()
    $(menu).slideToggle();
    var text = $(menu).is(':visible') ? 'open' : 'close';
    $('.sub-menu-status').text(text);
  });

For some reason it only outputs close it never writes open.

Inspected code and tried this variation but get the same result as above:

  var text = $(menu).css('display') == 'none' ? 'open' : 'close';

Can someone help me understand this? Thanks.

jsFiddle update: http://jsfiddle.net/#&togetherjs=LlkSSawSsb

MrPizzaFace
  • 7,807
  • 15
  • 79
  • 123

1 Answers1

0

From this question I get that is(':visible') will be checking when the slideToggle transition starts. So it always returns true.

Try this

$('.sub-menu').on('click', function(){
    var menu = $(this).parents('.row').next()
    var text = $(menu).is(':visible') ? 'close' : 'open';
    $(menu).slideToggle();   
    $('.sub-menu-status').text(text);
  });

DEMO

Or DO it in callback of slideToggle

 $('.sub-menu').on('click', function(){
    var menu = $(this).parents('.row').next()
    $(menu).slideToggle(function() {
        var text = $(menu).is(':visible') ? 'open' : 'close';
        $('.sub-menu-status').text(text);
    })
  });

DEMO

Community
  • 1
  • 1
Mritunjay
  • 25,338
  • 7
  • 55
  • 68