views:

46

answers:

0

Hi All, I need to implement an autocomplete text field using JavaScript and JSF. When the user types some character in a text field, I need to make an AJAX request and get the values based on the values entered.

I've an input text field, on "keyup" I'm triggering a function which submits the value to the server side.

  var timeoutid = 0;
  function intitiateTypeAhead(){    
    clearTimeout(timeoutid);
      if (document.getElementById("inputText").value.length >= 2) {
    timeoutid  = setTimeout('clickButton', 500);
    }

    return false;
  }
 function clickButton(){
      //submits the value to the server
}
<h:inputText id="inputText" onkeyup="intitiateTypeAhead()"></h:inputText>

This works properly, but certain times the request is made for each character entered by the user. I'm not sure if there is anything wrong with the implementation. Can someone please help me to fix this?