views:

36

answers:

1

Hello guys!

Here's my code:

function hideColumnAndShowOther(columnToHide, columnToShow) {
$(columnToHide).fadeTo("slow", 0.0, function() {
                       $(columnToShow).fadeIn("slow");
                       });
}

In this case the callback function isn't called. I have used the firebug tool to get the root of the problem. In the callback function the 'columnToShow' variable is not present. I think it is logical because it is a separate block, but how can I solve this then? Do you have any tip?

Thanks!

+1  A: 

The problem is the columnToShow variable is out of scope. It means nothing at that point. Instead try $(this).fadeIn("slow").

The $(this) refers to the element that you have just faded as the callback function is attached to that element.

EDIT: (misread the question)

If you change it something like the following it should work.

function hideColumnAndShowOther(columnToHide, columnToShow) {
    var showColumn = columnToShow;
    $(columnToHide).fadeTo("slow", 0.0,
        function() {
           $(showColumn).fadeIn("slow");
        });
}
laurencek
but this doesn't solve my problem... I would like to fadein the columnToShow when fadeTo is finished.
Infinity
Sorry I misread the question. But the reason is still correct that the variables are now out of scope.
laurencek
If you create a new variables like var showColumn = columnToShow before you fadeOut the other column, then you can use showColumn in the callback.
laurencek
thanks, this was the solution. :)
Infinity
yea the first thing I did was use a global var but then your code works on jsFiddle without any changes? Or it doesn't work in browsers?
giddy
I tried it in firefox and safari and it isn't working, but with this var thing, it is working.
Infinity
I think this works in all browsers. Just quickly tested it in IE6+, chrome and firefox.Basically the var showColumn becomes global inside that main function, while the parameters get forgotten as they are not passed to the callback.
laurencek
@laurencek you mean his original code right? something is amiss! lol
giddy