5

I'd like to get some input from the user:

keywords = prompt("Input keywords separated by commas", "");

I have many various strings stored in an SQLite database that could suggest the user what to type. Let's say you have an array or list of these strings. How would you code this hinting in Javascript? Are there some functions or code snippets for this functionality?

I'd like it to work similarly as here. When you start typing you get hint with possibilities. There is only one disadvantage, that you can't input more strings separated by commas. Here is this feature working with more strings.

Is prompt() function suitable for this purpose? You can use another way of getting user input.

Thank you

xralf
  • 3,312
  • 45
  • 129
  • 200

2 Answers2

3

You cannot tweak the native prompt() javascript method.

I know you did not tag with jQuery but would be way easier to use a library to implement such a behavior.

You'll have to build your own dialog system using maybe jQuery UI Dialogs. They have an option to make it modal so the UI is blocked until the dialog is closed.

jQuery UI Dialog will not block javascript execution though like prompt does. You might need to be able to execute code when the dialog is closed. This answer show a way to implement that easily.

Finally, the jQuery UI Autocomplete provides an example on how to use it for multiple values in the same input. They use a comma-separator but I guess you could modify the example to work with whitespaces: jQuery UIAutocomplete Multiple Values

Community
  • 1
  • 1
Didier Ghys
  • 30,396
  • 9
  • 75
  • 81
  • thank you for answer. I like the last link you posted. The snippet will be part of firefox extension, so some kind of dialog box is needed. But I have minimal experience with *JQuery* so it would take me a few days to learn many other things. – xralf Mar 30 '12 at 09:20
  • Buidling that with pure javascript could take even more time I guess. Using jquery you'll just have to learn an API, the dirty stuff already being handled by the library and the plugin. Besides it is pretty straighforward. – Didier Ghys Mar 30 '12 at 09:23
  • OK, you are probably right. I will try to put it together with my existing addon code. – xralf Mar 30 '12 at 09:28
  • I have strongly stuck with *JQuery*, no method of including it to firefox extension from [this](http://stackoverflow.com/questions/491490/how-to-use-jquery-in-firefox-extension) page works for me. – xralf Mar 30 '12 at 17:31
2

You can't really use any custom functionality with prompt().

You'd be better off creating an <input type="text"/> and hooking the whole thing up to a button, making the data get submitted whenever a use clicks it, or presses enter. Then code it to fetch an autosugges value whenever the user types in a new character.

You should take a look at jQuery UI's autocomplete component (it works with multiple strings as an input as well). You would also need to set up a server-side script that will take a possibly incomplete string as an input and output a possible list of matches back to the browser.

Andrei Bârsan
  • 3,473
  • 2
  • 22
  • 46
  • It should be universal not only for one page, it will be Firefox addon. – xralf Mar 30 '12 at 09:16
  • jQuery should also work with manipulating XUL, see http://stackoverflow.com/questions/693174/is-it-possible-to-use-jquery-to-manipulate-xul-elements – Andrei Bârsan Mar 30 '12 at 14:31