0

I have made this code to resize ALL divs when one div is greater than other divs. The code executes on load then I want my nested function to work when window resizes. I am getting an error that resizeAgain() is not defined.

Updated: Forgot callback: sameSize('.content', '.col1', '.col2');

    <body onresize="resizeAgain()">
         function sameSize(e, selector1, selector2) {

        var slave = $(selector1).height();
        var master = $(selector2).height();
        var maxHeight = Math.max(slave, master);

        $(e).height(maxHeight);

        function resizeAgain() {
            $(e).css('height','');
            $(e).height(maxHeight);
        }
        sameSize('.content', '.col1', '.col2');
theoretisch
  • 1,718
  • 5
  • 24
  • 34
Darth Vader
  • 455
  • 1
  • 6
  • 17

2 Answers2

0

The function resizeAgain() is defined in sameSize(e, selector1, selector2) and this changes its scope making it inaccessible outside sameSize(..).

You can find more information at this other answer: javascript-nested-function

Community
  • 1
  • 1
Roberto Russo
  • 834
  • 10
  • 22
0

The function resizeAgain is only visible inside the scope of the sameSize function, that's why you have an error when you call it from the window scope.

You can define this function in the global scope :

<body onresize="resizeAgain('.content')">

function resizeAgain(e) {
  $(e).css('height','');
  $(e).height(maxHeight);
}

function sameSize(e, selector1, selector2) {
   var slave = $(selector1).height();
   var   master = $(selector2).height();
   var  maxHeight = Math.max(slave, master);
   $(e).height(maxHeight);
}

sameSize('.content', '.col1', '.col2');
Olivier Boissé
  • 15,834
  • 6
  • 38
  • 56