How to add an autocomplete field to a form

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!

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

How to add an autocomplete field to a form

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.