How to Create an Ajax Autocomplete Text Field: Part 11 [con't]
Keeping the List Open on Page Load
Once you've conducted a search, clicked on a list item, or simply gone to another URL, and returned to the
AutocompleteCSS.jsp page, you're likely to observe one of two behaviors: Either the search string will still be in the
autoComplete text field and the list will have vanished, or the list will remain open and the text field will have lost its contents. Since neither of these behaviors are desirable, let's see if we can't keep both the text field and list in the same state as we left them.
To bring back the list, add the following line to perform the search using the
autoComplete text field value. The
if statement tests for any non-blank value:
AUTOCOMPLETE Parameter Bug
The second issue was a lot harder to track down because it wasn't the default browser behavior. An exhaustive search revealed that the
AUTOCOMPLETE attribute was the culprit. Although most browsers do recognize it, there's no standardized compliance for it. Firefox, for example, will only recognize it at the form level, while Internet Explorer supports it at the element level. You would expect that browsers who don't recognize the
AUTOCOMPLETE attribute would simply ignore it, but it caused my Firefox version 2 to forget the text field's contents between page loads.
It's obvious that the
AUTOCOMPLETEattribute has got to go. Remove the
AUTOCOMPLETEattribute from the
complete-fieldinput element in the
Autocomplete.jsppage as follows:
Many developers have tried to devise a way to circumvent JSP validator errors caused by the
AUTOCOMPLETE attribute to the
<form> tag in the
Here's a zip archive containing the two files that we modified today.
That's a wrap on our Autocomplete Control. I hope you learned a lot from this series. At the very least, I think that it demonstrated how Web development is about getting a lot of different technologies, tools and languages to work together to solve a problem. It's important to not get fixated on the same approach for every problem and to keep your ear to the ground for new developments. Perhaps you'll be inspired to contribute to those developments. That's what open source is all about!
- Introduction to Java event listeners
- DOM events
- More on DOM Events!
- Canceling events
- Turning off autocompletion in Mozilla-style browsers
Original: August 14, 2008