2

I know that stackoverflow is not to do my job, for that I ask for generics advices

  • my autocomplet will be in a input, from what I know, input has "get" and "post" request. I need to make a post request, and here is my problem. My autocomplete read information from a endpoint JSON, that is generated when you make a request from server, e.g. http://www.imobiliare.ro/sugestii/huned, I need a javascript function, that is continuous read and send information from input text to URL.

E.g

I have :"http://www.imobiliare.ro/sugestii/huned"+ T , T value from input, Information is pass to URL and generate a JSON, after that http://www.imobiliare.ro/sugestii/huned+T+I, TI values from input.

Hard part for me is how to send continuous that information to url without press a submit button, something like every time I pass a value in input text, is read and send automaticali to URL and make a request

I can't use any existing lib.

2 Answers 2

2

Here is some starting points for you, which you should already know if you took a task which says "do not use any libraries". Autocomplete "without using any libraries" is wrong design requirement and a waste of time in almost all cases imaginable.

  1. Bind a listener on "keyup" event.
  2. This listener should fire not each time "keyup" is fired, but with delay of ~500 ms, or else you'll be triggering POST requests on each letter entered, and in case of "deoxyribonucleic acid" will be probably overkill.

That's all. You absolutely do not need any "continuous" checking, only key presses.

For "continuous" checking you should use setTimeout and setInterval functions.

EDIT

Here is JsFiddle with the crude solution showing both of my points: you attach an event listener to the text input and then (with proper debouncing mechanics!) on key presses you send a request to your backend API. Everything else is up to your imagination. Usually results are returned into a dropdown list, but I certainly would not reimplement that from scratch.

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

4 Comments

I very new in javascript, I tried to found examples with what you told me, but I was unlucky, Can you help me with a littel example ?
If you need an example, I would suggest you to read sources of the various javascript autocomplete plugins out there at Github. Code there varies in level of badness but all this is production code nevertheless.
Also, you asked for "generic advices" in your question and now you ask for example of code. ;P
Look at how much code is in codebase of Twitter's autocomplete and in native Javascript autocomplete.js libraries. Are you sure you want to re-implement all this functionality from scratch?There's a lot of code just for usability improvements, though.
0

this might help you Building an Autocomplete from scratch with javascript

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.