27

How can I bind an OnChange event of a TextBox to function using jQuery?

I am trying this, and it’s failing:

$(document).ready(function() {
    $("input#tags").bind("change", autoFill);
});

function autoFill() {
    $("#tags").autocomplete("/taglookup/", {
        width: 320,
        max: 4,
        highlight: false,
        multiple: true,
        multipleSeparator:",",
        scroll: true,
        scrollHeight: 300,
        delay: 10
    });
}

NB: I want to implement a Tag TextBox like the one for Stack Overflow which detects OnChange events and calls Ajax functions when a user types in.

3
  • 1
    What does exactly fail? Is the autofill not called or is it something else? Commented Apr 30, 2009 at 8:52
  • For a textbox with an autocomplete functionality, it's probably better onkeyup event. Commented Apr 30, 2009 at 8:57
  • @Eddy, why is that? Wouldn't you be better off sending the AJAX request of asap? Commented Sep 6, 2010 at 17:52

7 Answers 7

31

You're looking for keydown/press/up

$("#inputID").keydown(function(event) {
    alert(event.keyCode);
});

or using bind $("#inputID").bind('onkeydown', ...

http://docs.jquery.com/Events

Sign up to request clarification or add additional context in comments.

3 Comments

I found that using keydown meant that my handler would fire on certain weird keystrokes such as ALT+TAB.
Key* only works for keypresses. If you paste something in the textbox using the mouse context menu it won't fire. Using an onchange event is usually the way to go.
@ingredient_15939 which is why my answer specifically mentions "If you're looking for KEYdown/press/up", not talking about the mouse events
18

You must change the event name from "change" to "onchange":

$(document).ready(function(){
        $("input#tags").bind("onchange", autoFill);
          });

or use the shortcut binder method change:

$(document).ready(function(){
        $("input#tags").change(autoFill);
          });

Note that the onchange event usually fires when the user leave the input, so for auto-complete you better use the keydown event.

Comments

7

Combination of keyup and change is not necessarily enough (browser's autocomplete and paste using mouse also changes the contents of a text box, but doesn't fire either of these events):

jquery change not working incase of dynamic value change

Comments

1

Here's a clue why an onchange() call might not fire your bound function:

http://jehiah.cz/archive/firing-javascript-events-properly

Comments

1

I second Chad Grant's answer and also submit this blog article [removed dead link] for your viewing pleasure.

3 Comments

That link (themaingate.net/dev/javascript/…) appears to be dead.
ONOZ. I hate it when that happens. I will remove the link. :(
There isn't any value in this answer. It ought to be deleted.
1

What Chad says, except it’s better to use .keyup in this case, because with .keydown and .keypress, the value of the input is still the older value, i.e., the newest key pressed would not be reflected if .val() is called.

This should probably be a comment on Chad's answer, but I don’t have privileges to comment yet.

Comments

0

If you're trying to use the jQuery autocomplete plugin, then I think you don't need to bind to onChange event. It will.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.