Cleanly binding to form elements

Instead of getting messy Javascript all over the place, a smarter way to bind AutoSuggest to your form elements is to use a separate javascript file to connect AutoSuggest to your text inputs procedurally:

		function bindEvents()
		{
			//Find all of the INPUT tags
			var tags = document.getElementsByTagName('INPUT');
			for (i in tags)
			{
				var tag = tags[i];
				//If it's a text tag, attach an AutoSuggest object.
				if(tag.type.toLowerCase() == "text")
				{
					new AutoSuggest(tag,fruits);
				}
			}
		}
		
		window.onload = bindEvents;
	

You can, of course, modify this to bind by id, or any other method you might want. It's easier to maintain because the code is only in one place, and you don't have all that script in your HTML.

Fruit

What's your favorite fruit for breakfast?

The best fruit on pancakes?

The best fruit for jams and jellies?

< Back