Hi all,

not sure where to best post this, as the autocomplete.js comes with core rather than a specific module. I'm using the autocomplete functionality on a dynamic list, where elements should be added once an item is selected from the autocomplete list or a custom entry is written into that (autocomplete) textfield and enter is hit.
What I originally tried to do is to use the onsubmit() function on that textfield to handle the dynamic list creation, but autocomplete seems to disable that from working, so I'm a bit stuck as how to elegantly solve this problem.

Well, I've fixed it now by severely hacking into the autocomplete.js file, my new code is below:

Drupal.autocompleteSubmit = function () {
  if(focusedName == 'new_ingr_text') {
    Drupal.autocompleteSubSubmit(); // Close pop-up
    if(document.getElementById('edit-new-ingr-text').value.length > 0) {
      MyScripts.addIngr(); 
      document.getElementById('edit-new-ingr-text').value = "";
}
    return false;
} else {
    return Drupal.autocompleteSubSubmit(); 
}
}

Drupal.autocompleteSubSubmit = function () {
  return $('#autocomplete').each(function () {
    this.owner.hidePopup();
}).size() == 0;
}

focusedName = new_ingr_text whenever that field is focused. Basically, whenever that field is in focus, I don't want the form to submit at all, but only to (depending on the fields value) to run my custom addIngr() function.
Anyway, I was wondering if there is a more elegant solution to this issue, avoiding to hack into core code? Can I somehow attach some extra javascript to be executed on every submit (in which I could then do all the above checks)? I.e. is the a Drupal alternative to the onsubmit() function, which autocomplete seems to break?

Thanks!
kmh

Comments

Dave Cohen’s picture

I have a similar need to customize autocomplete, and I don't know how to do it elegantly, either. Did you have any luck not hacking core?