Skip to content

Instantly share code, notes, and snippets.

@bakpa79
Last active September 9, 2015 21:28
Show Gist options
  • Select an option

  • Save bakpa79/f7296a1a0a6985fd480c to your computer and use it in GitHub Desktop.

Select an option

Save bakpa79/f7296a1a0a6985fd480c to your computer and use it in GitHub Desktop.
// inputEl is the search <input> element,
//predictiveItemsEl is the container element for the predictive search list,
//predictiveItemsListEl is the <ul> list itself.
//I use this inside an object created from the search element.
function submitPredictivetext(inputEl, predictiveItemsEl, predictiveItemsListEl) {
var $this = jQuery(inputEl),
thisValue = $this[0].value,
thatValue="";
var timer = $this.data('timeout') || 500,
inputTimeOut;
// window.clearTimeout(inputTimeOut);
inputTimeOut = window.setTimeout(function(){
thatValue = $this[0].value;
console.log('fire service call with: ', thisValue, thatValue);
if(thisValue == thatValue) {
if ($this[0].selectionEnd > 2){
if (!predictiveItemsEl.hasClass('active')){
predictiveItemsEl.addClass('active');
}
// Clear and reset the list
predictiveItemsListEl.find('li').remove('li');
mkServices.predictiveSearchService({
'searchQuery': this.value,
'predictiveListElement': predictiveItemsListEl
});
} else if($this[0].selectionEnd <= 2 && predictiveItemsEl.hasClass('active')) {
// Clear and reset the list
predictiveItemsListEl.find('li').remove('li');
predictiveItemsEl.removeClass('active');
}
}
}, timer);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment