0

I want to change css properties inside of a named function thant handles the click event. If I call .css in an anonymous function defined in the click event, it works. However, I want to call .css inside of a named function defined inside the handler function. Here's my code.

$(document).ready(function () {
    $('#popup').click(partial(coolTransition, $(this)));
});

function coolTransition(elem) {
    shrink();

    function shrink() {
        elem.css('background-color', '#000000');
    }
}

//used to implement partial function application so that I can pass a handler reference that takes arguments. 
//see http://stackoverflow.com/questions/321113/how-can-i-pre-set-arguments-in-javascript-function-call-partial-function-applic
function partial(func /*, 0..n args */ ) {
    var args = Array.prototype.slice.call(arguments, 1);
    return function () {
        var allArguments = args.concat(Array.prototype.slice.call(arguments));
        return func.apply(this, allArguments);
    };
}

Thanks in advance for help.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Vish
  • 453
  • 6
  • 18

2 Answers2

2

What do you think $(this) refers to in the second line? It is not $('#popup'). If you want to refer to it, you have to create a reference first:

$(document).ready(function () {
    var $popup = $('#popup');
    $popup.click(partial(coolTransition, $popup));
});

That said, using partial is not necessary. You could also do:

$('#popup').click(function() {
    coolTransition($(this));
});

or even

$('#popup').click(function() {
    $(this).css('background-color', '#000000');
});

And there are many other ways...

Btw. defining shrink inside coolTransition seems to be unnecessary. Don't do it unless you have a reason.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
2

In:

$(document).ready(function() {
    $('#popup').click(partial(coolTransition, $(this)));
});

$(this) is $(document). You need $(this) to take on the context of $('#popup'), and for this you need a function:

$(document).ready(function() {
    $('#popup').click((function() {
       return partial(coolTransition, $(this));
    })());
});

Alternatively, you can just avoid $(this) entirely:

$(document).ready(function () {
    var $elm = $('#popup');
    $elm.click(partial(coolTransition, $elm));
});

Really, though, this whole usage of partial seems overly complex. Why do you think that you need it? The easiest approach is:

$(document).ready(function() {
    $('#popup').click(function() {
       coolTransition($(this));
    });
});
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • I was using partial to let me pass a function by reference with arguments. I found it in another stack overflow post, but what you did is much cleaner, so I'm going to use that. Thanks! – Vish Sep 11 '11 at 17:25
  • @Vish: Yea, you can just wrap a function call in another function expression to do that. :) I edited my answer to demonstrate this. – Lightness Races in Orbit Sep 11 '11 at 17:44