Overriding autocomplete functions

  1. var myinput = $('#edit-name1').attr('autocomplete', 'OFF')[0];
  2. var db = new Drupal.ACDB('http://localhost/myproject/?q=my/autocomplete');
  3. var x = new Drupal.jsAC(myinput, db);
  4. x.found = function (matches) {
  5.   // If no value in the textfield, do not show the popup.
  6.   if (!this.input.value.length) {
  7.     return false;
  8.   }
  9.  
  10.   // Prepare matches
  11.   var ul = document.createElement('ul');
  12.   var ac = this;
  13.   for (key in matches) {
  14.     var li = document.createElement('li');
  15.     $(li)
  16.       .html('<div>'+ matches[key]['display'] +'</div>')
  17.       .mousedown(function () { ac.select(this); })
  18.       .mouseover(function () { ac.highlight(this); })
  19.       .mouseout(function () { ac.unhighlight(this); });
  20.     li.autocompleteValue = key;
  21.     li.info = matches[key]['info'];
  22.     $(ul).append(li);
  23.   }
  24.  
  25.   // Show popup with matches, if any
  26.   if (this.popup) {
  27.     if (ul.childNodes.length > 0) {
  28.       $(this.popup).empty().append(ul).show();
  29.     }
  30.     else {
  31.       $(this.popup).css({visibility: 'hidden'});
  32.       this.hidePopup();
  33.     }
  34.   }
  35. }
  36.  
  37. x.select = function (node) {
  38.   this.input.value = node.autocompleteValue;
  39.   this.info = node.info;
  40.   var newdiv = $("<div></div>").addClass("messages status")
  41.   .html("OK.");
  42.   $("input#edit-email1").parent().fadeOut("slow").after(newdiv);
  43.   $("#edit-uid1").val(this.info['uid']);
  44. }