0

I have an input which changes value when a click a button. What I want to do is to force an enter keypress event with jQuery. without pressing the button. Is this possible?

Thanks

3 Answers 3

7

jQuerys .trigger() should do the trick:

$('input').trigger({
    type: 'keypress',
    which: 13
});

Ref.: .trigger()

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

1 Comment

cheers jAndy, this helped me chain a button to hit enter on an input box
1

$(element).keydown(function(event) { if (event.keyCode == '13') { event.preventDefault(); } do_something(); });

2 Comments

I think it doesn't work as well. I don't want to capture the keypress and do something. I want to force an enter keypress with code.
Ok. Is running $(element).keydown() on your element enought? You won't get the event id this way but a general keydown(). I don't think it's possible to set event id. May .submit() be close enough to simulate a event with id 13 maybe?
-1

I needed to use this on a search box to clear it's content...but instead of using 'keypress' or 'keydown', I needed to use 'keyup' so that the search function would fire also. This way I could clear the limiting search results.

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.