0

I have a custom search field which searches for a type of content depending on the radio box selected. I have applied autocomplete functionality to the field using hook_form_alter but only want the autocomplete functionality to be available for one of the search types.

Is it possible to conditionally detach the Drupal.behaviors.autocomplete from the text field using my own js?

Update: I have managed to disable the autocomplete functionality by unbinding the events, but I do not know how to add/bind it again:

// update search box text when search type is changed
$(SI_SEARCH.radio).change(function() {
  // get search type
  SI_SEARCH.search_type = $(this).val().trim();
  switch(SI_SEARCH.search_type) {
    case 'course' :

      break;
    case 'university' :
      // disable autocomplete
      $('#edit-search').unbind('keydown keyup blur');
      break;
  }
})

1 Answer 1

0

Ok I managed to achieve this after digging into misc/autocomplete.js

To disable the autocomplete you can do the following:

$('#edit-search').unbind('keydown keyup blur');
$('#[AUTOCOMPLETE-ELEMENT-ID]-autocomplete-aria-live').remove();
$('#[AUTOCOMPLETE-ELEMENT-ID]-autocomplete').removeClass('autocomplete-processed');

To enable the it again:

Drupal.behaviors.autocomplete.attach(document);

My js ended up as:..

// update search box text when search type is changed
$(SI_SEARCH.radio).change(function() {
  // get search type
  SI_SEARCH.search_type = $(this).val().trim();
  switch(SI_SEARCH.search_type) {
    case 'course' :
      // re-attach autocomplete
      Drupal.behaviors.autocomplete.attach(document);
      break;
    case 'university' :
      // disable autocomplete
      $(SI_SEARCH.search_form).next().removeClass('autocomplete-processed');
      $(SI_SEARCH.search_form).unbind('keydown keyup blur');
      $('#edit-search-autocomplete-aria-live').remove();
      break;
  }
})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.