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
  • 756,363
  • 169
  • 1,062
  • 1,111
oliverwebr
  • 247
  • 2
  • 11

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
  • 291,719
  • 55
  • 540
  • 600
  • 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,619
  • 7
  • 54
  • 93