9

I did my homework and checked lots of questions but still couldn't find something that I can get to work.

I have a search input. When user clicks on example, I want the word "doctor" be written in my search input letter by letter. I could have done that by changing $("#search").val() long ago, but here is the problem. When normally user types into search input autocompletion div is coming out. It is triggered by keydown event. If i just change the val attribute of input, the autocomplete div won't come out, so I need to somehow trigger keydown event so it will come out.

I found this solution:

$("#example").click(function() {    
    $("#search").focus();
    var e = jQuery.Event("keyup");
    e.keyCode = 50;                     
    $("#search").trigger(e);                    
});

but I couldnt get it to work. Any ideas please?

Here is how html looks:

<input type="text" id="search" />
Example: <span id="example">doctor</span>
2
  • My title says keydown event, but apparently that can only be achieved with keyup event, so in the code I used keyup instead. Anyways, none is working for me. Commented Sep 15, 2011 at 7:21
  • I also tried to use e.which instead of e.keyCode there. Still nothing happens to change. The script works untill the focus() event. The input gets focused and script appears to stop executing. Commented Sep 15, 2011 at 7:25

3 Answers 3

9
$("#example").click(function() {    
    $("#search").focus();
    var e = jQuery.Event("keydown");
    e.keyCode = 50;                     
    $("#search").trigger(e);                    
});

ref: Definitive way to trigger keypress events with jQuery

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

1 Comment

I guess I found an answer that works for me from the link that you provided, though I looked at it before I never noticed one comment. Quoting comment of @Nadia Alramli: > No you miss understood the concept. This is not how is it supposed to > work. trigger will only call the event handler. It will not actually > print the key. If you want to simulate the effect of printing the key, > then just add the key to the input value and trigger the event at the > same time. – Nadia Alramli May 7 '09 at 0:21
0

A better way is to trigger your autocomplete on a textChange event rather than keyup/down. That way you can use $("#search").val() the easy way and the autocompleter will be triggered.

Added bonus: If you use textChange it will also trigger the autocomplete if the user copy-pastes text using the mouse context menu, etc. (and it won't trigger on tabbing or arrow keys)

3 Comments

Thanks for the response! I surely will check this out. I didn't know such an event is possible. However I filtered my keydown so if arrows are pressed, it won't fire autocomplete.
I just realized that I need to trigger keypress anyway, because when user will press on example, the doctor word will be typed, and the autocomplete div will come out. Then I will need to trigger down arrow once and enter key once so it will go to the url of the first suggestion in autocomplete div. It is supposed to be something like a demo.
Sure, but if it were me, I would do .textchange(autocomplete_handler) and a separate .keydown(arrows_and_enter_handler)
0

If you are using jQuery autocomplete, forcing a keypress doesn't do the trick. Force an autocomplete. You might have to delay it a bit:

setTimeout(function()
{
    var CurrentVal = $("#example").val();
    $("#example").autocomplete( "search", CurrentVal )
},500);

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.