Question

So in my Javascript, I have this

$('ul li').click(function(){ //loops through all <li>'s inside a <ul>

    $('ul .clicked').removeClass('clicked'); // when an <li> is clicked, remove .clicked class from any other <li>'s
    $(this).addClass('clicked'); // add .clicked class to the clicked <li> ($(this))

    $(this).screenSlide();
});

Now, the screenSlide function is this

$.fn.screenSlide = function(){ // $(this) = aboutSectorNineteen (<li>'s id)

    var test = $('.current').attr('id'); //if an element with .current as it's class exists, let test be equal to that elements id
    test = "#" + test;
    $(test).slideUp(); // slide it up, hide it and remove the .current class from the <li> element
    $(test).hide();
    $(test).removeClass('current');
    var gettingShown = $(this).attr('id');
    gettingShown = "#" + gettingShown + "Slide";
    $(gettingShown).addClass('current'); // add the .current class to $(this) <li>
    $(gettingShown).slideDown();
};

Now, gettingShown does slide up and when I click another < li > then the screen which slid up (gettingShown) does hide, but it doesn't slideUp. Which means that

$(test).hide();

is working however

$(test).slideUp();

is not working, right? Why is that? I also tried changing slideUp to fadeOut but that still didn't work. I change slideDown to fadeIn and it worked. How come slideUp and fadeOut aren't working? Am I using it incorrectly?

Was it helpful?

Solution

slideUp() is async, and it hides the element on completion of sliding up.

It should be

$(test).slideUp(function () {
    $(this).removeClass('current');
});

OTHER TIPS

This is a cleaner version of the bound event and actions.

$('ul > li').click(function() { //loops through all <li>'s inside a <ul>
    $('li').removeClass('clicked'); // when an <li> is clicked, remove .clicked class from any other <li>'s
    $(this).addClass('clicked').screenSlide(); // add .clicked class to the clicked <li> ($(this))
});

$.fn.screenSlide = function() { // $(this) = aboutSectorNineteen (<li>'s id)
    var test = $('.current').attr('id'); //if an element with .current as it's class exists, let test be equal to that elements id
    $('#' + test).slideUp().removeClass('current'); // slide it up, hide it and remove the .current class from the <li> element
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top