Hi there!

I've a Drupal textfield with a #autocomplete_path attribute. When the user add's a value to that field I would like to change the autocomplete path. I would like to add the value of an other field to the Ajax call that will be made. For example: album/autocomplete/work_by_composer/input-of-this-field/input-of-previous-field

I tried this with jQuery without success:

if (Drupal.jsEnabled) {
  $(document).ready(function() {
    $("#edit-composer").change(function() {

      var composer = $("#edit-composer").val();
      $('#edit-work-autocomplete').val("http://example.com/album/autocomplete/work_by_composer/" + composer +  "/");
    })
  })
}

Can somebody help me with this?

Comments

Floris246’s picture

My colleague just found a solution:
The class "autocomplete-processed" has to be removed and the autocomplete field has to be reset as well.

if (Drupal.jsEnabled) {
  $(document).ready(function() {
    $('#edit-composer').change(function() {
      var composer = $('#edit-composer').val();
      $('#edit-work-autocomplete').val('/album/autocomplete/work_by_composer/' + composer);
      $('#edit-work-autocomplete').removeClass('autocomplete-processed'); 
      Drupal.behaviors.autocomplete(document);
    })
  })
}
kpv’s picture

Found the same solution. It works but it still makes calls to the previous uri. Thus, e.g. you had 'path/autocomplete/1' that returned abd abde for 'ab' and you dynamically change it to 'path/autocomplete/2' that returns abcd abcde for 'ab'. In that case you will still get abd abde for 'ab'. That's because the old event is fired first.
To escape this you need to unbind the event from the textfield (not hidden value). In your case it is $('#edit-work').unbind().val(''); Here we also clear preselected value.

For more detail see, for ex., location_autocomplete.js (http://bak.ndla.no/cgi-bin/trac.cgi/browser/modules/3rdparty/drupal/loca...)
Tnx to http://stackoverflow.com/users/9143/john-fiala

succinct’s picture

I've tried using this approach to change my autocomplete path, but I'm getting this error constantly:

Uncaught TypeError: Property 'autocomplete' of object #

is not a function
Also, Drupal.jsEnabled has been deprecated, what version was your solution for?
alexkb’s picture

I was attempting this in Drupal 7, and had the same issues, succinct, so ended up trying a different approach.

Instead of trying to change the autocomplete url, you can override drupal's autocomplete behaviour and do things differently.

This blog post explains how to do it for Drupal 6, but the approach is the same, with slightly different code (copied from autocomplete.js): http://sachachua.com/blog/2011/08/drupal-overriding-drupal-autocompletio...

If anyone knows a better approach, please post back.

Joe Vallender’s picture

I'm using this successfully on a Drupal 7 site (well, it's edited to make sense out of context)


$('#hidden-field-holding-the-url')
  .removeClass('autocomplete-processed')
  .val('/new/path');

$('#text-field-with-autocomplete')
  .unbind();

Drupal.behaviors.autocomplete.attach(document);

I was caught out at first by spelling behaviours the English way, with a 'U' :-)

oman117’s picture

thanks to Joe for posting his piece.

$('#hidden-field-holding-the-url')
.removeClass('autocomplete-processed')
.val('/new/path');

$('#text-field-with-autocomplete')
.unbind();

Drupal.behaviors.autocomplete.attach(document);

--
I was banging my head against this trying to get it to work and that last piece was the key.