0

I am using EasyAutocomplete, it works well, only problem I am facing - Input box loses control when EasyAutocomplete is attached to it.

I want EasyAutocomplete to get activated after the user has typed 2 characters.
When I type 1 character nothing happens as needed, but after 2nd character has been typed EasyAutocomplete must get attached and should start working. However, what happens is I have to click outside of the input box to make things happen.
It is just this 'outside click', that I have to do to make this plugin work, is problematic for me.
I have tried input event as well but i did not work as required.
The change event seems quite suited for my requirement.
How do I solve this issue?

var ib = $("#inputbox");

$(document).on("keyup", ib,function(e) {
leng = ib.val();
 });


$(document).on("change", ib,function(e) {    
if(leng.length < 2){
       #do something      
 }else{
       ib.easyAutocomplete(options);
  }
 });
lmao
  • 452
  • 1
  • 5
  • 13

1 Answers1

0

First you should change "change" by "keyup"

Second leng doesn't update as you put it outside of the event

Lastly an event on document may not be the best if you want this event to trigger only on this element, change document by your element

$("#inputbox").on("keyup", ib,function(e) {
   if($("#inputbox").val().length < 2){
      #do something      
   }else{
      ib.easyAutocomplete(options);
   }
});
Romain B.
  • 656
  • 6
  • 15
  • Thanks for replying, Sir. I did not post entire code earlier, I have posted almost all the relevant things now. I am already using `keyup` for calculating `leng`, could you just check the edits once again and suggest if I should make some changes. – lmao Feb 27 '19 at 16:32
  • If `change` is not used, changes in input are not reflected properly. At first, I used keyup alone, but it did not work properly. I used `input` event as well but it seems to have problem too. Event `change` is working nicely, but that losing focus is something i can't seem to solve on my own. – lmao Feb 27 '19 at 16:35
  • Tried putting `ib.focus()` as well, it too doesn't work. – lmao Feb 27 '19 at 16:39