0

Hey guys I know that there exists .slideToggle() but I want to add more features later.

I don't know what I'm doing wrong, sliding up works but I cannot slide down.

Can I not overwrite my var? Would be very nice when somebody can help me.

$(document).ready(function () {

    var resizeValue = true;
    $(".resizeSelect").click(function () {
        if (resizeValue === true) {
            $(".resize").slideUp(

            function () {
                $('.parent').height($('.child').height('100'));
            });
            var resizeValue = false;
        } else {
            $(".resize").slideDown(

            function () {
                $('.parent').height($('.child').height('100'));
            });
            var resizeValue = true
        };
    });
});
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
oliverwebr
  • 459
  • 3
  • 13

2 Answers2

8

You shouldn't redefine the resizeValue variable within the click function. Just remove var from var resizeValue (it should only be used at the top of the ready-function).

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • This points to this answer about pass-by-reference in JS: http://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language – Alfabravo Feb 07 '13 at 14:51
6

Because you are redeclaring your variable resizeValue in your function instead of update it :

$(document).ready(function () {

    var resizeValue = true;
    $(".resizeSelect").click(function () {
        if (resizeValue === true) {
            $(".resize").slideUp(

            function () {
                $('.parent').height($('.child').height('100'));
            });
            //DO NOT DECLARE NEW VARIABLE WITH VAR
            resizeValue = false;
        } else {
            $(".resize").slideDown(

            function () {
                $('.parent').height($('.child').height('100'));
            });
            //DO NOT DECLARE NEW VARIABLE WITH VAR
            resizeValue = true
        };
    });
});
sdespont
  • 13,915
  • 9
  • 56
  • 97