ametren
ametren

Reputation: 2246

How do I get $(this) two levels deep?

alright so I'm trying to set up a keydown dispatcher that will do different things based on which key was pressed.

It's added to onkeyup on my input field

$(function() {
  $("#myInput").keyup(suggestion_dispatcher);
});

and then this is the suggestion dispatcher itself

function suggestion_dispatcher() {
    alert($(this).val());
    var code = event.which;

    if(code == 13) {
        // select
    } else if (code == 38) {
        // up
    } else if (code == 40) {
        // down
    } else {
        $.proxy(get_suggestion(), $(this));
    }
}

Even using the jQuery proxy, inside my get_suggestion() function if I look at this or $(this) I see that it's giving me the window object instead of the input box that's being typed into. What am I missing?

Upvotes: 1

Views: 81

Answers (1)

Liangliang Zheng
Liangliang Zheng

Reputation: 1785

You might want to try this : get_suggestion.apply(this); or get_suggestion.call(this);.

Here is some useful reference - http://odetocode.com/blogs/scott/archive/2007/07/05/function-apply-and-function-call-in-javascript.aspx

Upvotes: 4

Related Questions