1

suppose I have following string and following element:

var s = "aa"
<input id="inet-field" class="suggest-input js-suggest-input" ...">

Now what I want to achieve is that I want to dispatch some event which will invoke suggestionenter image description here

I tried following but without success:

 var doc = document.getElementById("inet-field");
 var kEvent = document.createEvent("Event");
 var s = "aa"
 for (i = 0; i < 2; i++){
     kEvent.initEvent("keypress", true, true);
     kEvent.keyCode = 94;
     doc.dispatchEvent(kEvent);
 } 

What I am doing wrong ? I am using Chromium browser.

Petr Krčmárik
  • 221
  • 1
  • 2
  • 13
  • Paste your code on fiddle – Man Programmer Oct 14 '15 at 07:53
  • The suggestion from google and other search engines doesn't come from a key pressed in runtime. What if the user don't want the runtime-pressed key? If I want to type `aab`, with your idea my result will be `aaab` – Cliff Burton Oct 14 '15 at 07:57
  • Ok, I will try to explain what I am really trying to achieve. What I am actually doing is developing some automation tool. Now suppose that user write text "aa" to this search field and then click on some suggestion. I cannot simply do `searchfield.value = "aa"`, because this will not invoke this sugestion and next step will fail. So I need to somehow handle this, like really simulate keypressing on that element. @CliffBurton – Petr Krčmárik Oct 14 '15 at 08:07
  • try to look some `autocomplete` js code – Anonymous0day Oct 14 '15 at 08:13

1 Answers1

0

Should using an external library be an option, I would like to suggest typeahead.js

It's easy to use and fast.

Daniël J
  • 107
  • 10