I've got a page that, in response to a button click, brings up a Bootstrap modal form. In that form is a textfield with the standard Drupal autocomplete behavior; it's working fine.

I now want to set things up so that, after a value is put into that field, another mostly-identical autocomplete field is added to the form -- the idea is to let the user specify additional values for the property being handled by this part of the form.

I've hung a jQuery blur handler off the textfield that takes the HTML that ends up in the form for the initial autocomplete form, parameterizes it a bit to avoid duplicate ID numbers, and inserts this updated field into the form when the first field gets a value. This is working fine from an HTML perspective, but the autocomplete behavior is not happening on the added form.

Minor notes:

  • After I insert the new field into the form, I re-run Drupal.attachBehaviors(), which I thought was going to make this all work. I've also added a delay to make sure the rewriting of the form is completed before attachBehaviors() is called, but that has no effect.

  • The autocomplete behavior continues to work on the original field after the new field is added to the form.

  • The blur handler that does all this stuff to the first autocomplete field is not firing when I blur control from the second, added field.

It looks like some other bit of initialization stuff needs to happen, but I have no clue what it is. Does anybody else? Thanks!


1 Answer 1


Well, that was pretty ugly, and a LOT of guesswork is going on here. But:

When Drupal sets up an autocomplete field, it adds a hidden variable to the form, which (I'm guessing here) is used to track the status of the whole autocomplete thing. If you inspect a rendered form, it will look something like:

<input type="hidden" id="edit-field-name-autocomplete" 
      value="https://example.com/path/to/autocomplete/handler" disabled="disabled" 
      class="autocomplete autocomplete-processed">

That autocomplete-processed field seems to be the key: I had blindly copied it into my code and made it part of the field I was added to the form. Once I removed it -- autocomplete-processed, that is -- autocompletion ran properly on it and all other similar fields.

Whew. Your mileage may DEFINITELY vary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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