ColdFusion 8 auto-suggest shows through other form fields

I was creating a form the other day that had 3 form fields, layed out vertically on top of one another. I switched the first field to be a cfinput, so I could use the autosuggest feature:

cfinput example 1

Then I switched the other two fields to be cfinputs so I could add autosuggest to them, too. And that is where I ran into problems:

cfinput example 2 - showing through

This happens in IE 6 and 7, but not FireFox. The problem occurs because when ColdFusion creates the <input> tag from a <cfinput>, it wraps it in two divs with a bit of related CSS. There is a conflict with the way ColdFusion sets the CSS float and z-index properties.

Thanks to my co-worker, CSS guru Jake Churchill, for figuring out a solution. All that needs to be done is to re-layer the inputs by setting their zindex. ColdFusion names the surrounding divs in an consistent way (thankfully they don’t use random names like some of the other ColdFusion JavaScript functions!), so we can just add a little CSS of our own to fix the problem. The divs end up named like myformfield1autosuggest, myformfield2autosuggest, etc. Knowing that, we can place this into our html page:

<style type="text/css">
	#field1autosuggest {
		z-index:3;
	}
	#field2autosuggest {
		z-index:2;
	}
	#test3autosuggest {
		z-index:1;
	}
</style>

Even better, here is a JavaScript script file you can reference in your page that will automatically fix these issues (thanks Jake!).

Download here

The script contents:

function fixAutoSuggest() {
	divs = document.getElementsByTagName("div");
	s = "<style type="text/css">\n";
	counter = 1;
	for (x = divs.length-1; x >= 0; x--) {
		if ( divs[x].getAttribute("id") && divs[x].getAttribute("id").indexOf("autosuggest") > -1) {
			s += "#"+divs[x].getAttribute("id")+" {z-index:"+(counter++)+"!important;}\n";
		}
	}
	s += "</style>";
	document.write(s);
};

You must then call this function after your form elements have been written to the browser. So - after your closing form tag, put this:

<script type="text/javascript">fixAutoSuggest();</script>

It does not work to simply call the function in the body’s onload attribute.

5 Responses to “ColdFusion 8 auto-suggest shows through other form fields”

  1. Axel Says:

    Thats Awesome.

  2. Jimmy Says:

    I used ColdFusion’s native Autosuggest for a bit but found jQuery to be much easier and extensible. Its a small footprint and the autosuggest plugin I used is based on ColdFusion examples. Even if you don’t use it, I think its worth a look:
    http://www.pengoworks.com/workshop/jquery/autocomplete.htm

  3. dickbob Says:

    Great fix guys!

    Any chance you could look at the same thing that happens when you set the type to “datefield”?

    The popup calendar that is generated bleeds all over the other input and select form fields. I’ve tried your technique but either it doesn’t work or I’m too dumb. I’m betting on the latter :-)

  4. Ryan Stille Says:

    We did spend a little time looking at the datefield, and its not an easy fix. For some reason they are writing the style for the date stuff inline, instead of using a class or ID selector.

    I’m really surprised Adobe overlooked this issue. Maybe they didn’t do any testing with more than one cfinput on a page?

  5. Peter Hopman Says:

    The issue was documented in the CF 8 release notes. Thanks for the fix.

Leave a Reply