tags:

views:

35

answers:

1

Hi,

I'd like to fire a custom event in the .text() function in jQuery. It seems like this should be possible but i'm having some trouble figuring out the closures(?). something like:


jQuery.fn.text = function(){
    _text = jQuery.fn.text;
    // fire a custom event/do something
    jQuery.apply(_text,arguments);
}

+2  A: 

I think you'll get an infinite loop (recursive) with that. You need to save a reference to the original text function before overwriting it. You also need to return the result of the call to _text.

_text = jQuery.fn.text;
jQuery.fn.text = function(){
    // fire a custom event/do something
    return _text.apply(this, arguments);
};
Joel Potter
good point. though making this change doesn't seem to affect any selected elements later on, running this in fire bug fails to change the posts:_text = jQuery.fn.text;$.fn.text = function(){ jQuery.apply(_text,arguments);}$("div.post-text").text("foo");
Christopher
I believe the error is in your application of `apply()`. See my edit. You also need to return the result of `_text` in your new function.
Joel Potter