1

I am trying to rebind the keydown event on focusout. Not really sure how what to pass to the keydown when rebinding. I tried passing this, but had no luck.

Anyone? Thanks

$('input.text').bind({
            click : function(e) { 

            },focusin : function(e) {

            },focusout : function() {
                // rebind keydown
                            // $(this).bind('keydown', this);
            },keydown : function() {
                $(this).unbind('keydown');
            }
slik
  • 5,001
  • 6
  • 34
  • 40
  • Great to see Rob's solution sorted you out, but my first thought was "I wonder if they really need to bind and re-bind or if they should be using .live(), .delegate() or .on(") (the last v1.7 onward). – Greg Pettit Nov 06 '11 at 17:26

2 Answers2

4

one of the possible solutions is to define the event function before calling the bind method on the element, and then reuse it to rebind when you focusout. it goes something like this:
(this code should work...)

keyDownFn = function() {
    console.log('this will happen only on the first keydown event!');
    $(this).unbind('keydown')
}

$('input.text').bind({
    click: function(e) {},
    focusin: function(e) {},
    focusout: function() { $(this).bind('keydown', keyDownFn); },
    keydown: keyDownFn
})

enjoy.

marxus
  • 462
  • 2
  • 6
3

You have to save a reference to the function. By saving a reference to the function, you can rebind the original function:

var keydown_func = function() {
    $(this).unbind('keydown');
};
$('input.text').bind({
    click : function(e) { 

    },focusin : function(e) {

    },focusout : function() {
        // rebind keydown
        $(this).bind('keydown', keydown_func);
    },keydown : keydown_func
}
Rob W
  • 341,306
  • 83
  • 791
  • 678