ColdFusion 8 Autosuggest bug to watch out for

This post is more than 2 years old.

Here is an odd bug a reader ran into this weekend involving ColdFusion 8's new autosuggest feature. It was difficult to debug but ended up being quite trivial.

The reader was not using an Ajax call to load the data for the suggestion list. Instead he performed a query, used valueList twice, and supplied those lists to two autosuggest cfinput fields.

One worked fine, but the other threw a JavaScript error. The problem ended up being his data. His list included a record that had double quotes in it. Here is a simple example:

<cfset list = "apple,orange,peaches ""and"" cream,klingon birds of prey">

In this list, the 3rd item, peaches "and" cream, includes quotes. Notice that I've escaped the quotes for ColdFusion to parse it correctly. When you run a page that makes use of this...

<cfform name="foo"> <cfinput name="test" autosuggest="#list#"> </cfform>

You end up with this generated in the browser:

_cf_autosuggestarray="apple,orange,peaches "and" cream,klingon birds of prey".split(",");

What's sad about this is that ColdFusion could easily handle this for you. In fact, it provides multiple options. One, toScript, handles the quotes just fine:

<cfset list = "apple,orange,peaches ""and"" cream,klingon birds of prey"> <cfoutput> <script> var #toScript(list,"mylist")#; </script> </cfoutput>

This returns:

<script> var mylist = "apple,orange,peaches \"and\" cream,klingon birds of prey"; ; </script>

See how it is escaped? This bug doesn't exist if yo use Ajax to load the data. If I change my input field to this:

<cfinput name="test" autosuggest="url:test3.cfm?v={cfautosuggestvalue}">

And then simply do:

<cfset list = "apple,orange,peaches ""and"" cream,klingon birds of prey"> <cfoutput>#serializeJSON(list)#</cfoutput>

It works fine. Something to watch out for folks! (And I'm going to file a quick bug report now.)

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA

Archived Comments

Comment 1 by Raul Riera posted on 11/3/2008 at 9:11 PM

Apart from the ":cfc" or ":url" are there others out there? Just curious

Comment 2 by Raymond Camden posted on 11/3/2008 at 9:28 PM

Rual, check the docs. Bindings support URL, CFC, and JavaScript connections.

Comment 3 by Raul Riera posted on 11/3/2008 at 9:38 PM

Its funny that you always say "Rual" Rya :P

Thanks, will look for the 3rd one

Comment 4 by Raymond Camden posted on 11/3/2008 at 9:44 PM

Doh. Sorry! :)

Comment 5 by Vince MacRae posted on 4/20/2011 at 5:41 PM

You're a saviour! Lost a goodly amount of time trying to work out why autosuggest worked with EVERY field in my table but the one I wanted - and sure enough when I read this and examined my data there were the double quotes. It doesn't appear to be docuemnted anywhere else and I really can't thank you enough!

Comment 6 by Raymond Camden posted on 4/20/2011 at 5:43 PM

You are most welcome.