user736893
user736893

Reputation:

manually trigger jquery keyup?

I think I figured it out... see my comment. Sorry.

I have a JSON web service that is called on a certain keyup() event which retrieves some data based on the input. I would like to load an initial set of data before anything is typed. I'm trying to manually invoke the keyup() as stated in the documentation, but it doesn't seem to do anything.

$('#inputItemName').keyup(function () {
//perform query, display results
});

^^ works great when you type in the box

$('#inputItemName').keyup();

^^ called immediately after document ready function, doesn't seem to do anything.

Upvotes: 58

Views: 105070

Answers (7)

user736893
user736893

Reputation:

I think I figured it out... I moved the function that calls the results higher up in the script tag than the keyup() invocation and now it works.

Upvotes: 2

richsoni
richsoni

Reputation: 4278

Try this:

$('#inputItemName').bind('keyup',function() {
    //stuff
});

Upvotes: -3

Piotr Perak
Piotr Perak

Reputation: 11088

Try trigger function.

$('#inputItemName').trigger('keyup');

http://api.jquery.com/trigger/

Upvotes: 7

Radu
Radu

Reputation: 8699

Works fine for me: http://jsfiddle.net/radu/gD5WL/

$('#test').keyup(function () {
    console.log('hello');
});

$('#test').keyup();

The keyup event is getting called on page load.

Upvotes: 90

Senad Meškin
Senad Meškin

Reputation: 13756

$('#inputItemName').trigger('keyup');

Upvotes: 38

Teja Kantamneni
Teja Kantamneni

Reputation: 17472

You are missing a # in selector... try with hash...

$('#inputItemName').keyup();

Upvotes: 3

glortho
glortho

Reputation: 13200

You're missing the hash marker for the id in the second one:

$('#inputItemName').keyup();

Upvotes: 1

Related Questions