WebReference.com logo
tip archive  •   about  •   sitemap  •   contact  •   jobs  •   write for us  •   subscribe


[previous] [next]

How to Create an Ajax Autocomplete Text Field: Part 11 [con't]

Technical Lead
Thomson Reuters (Markets) LLC
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

The ENTER Key

Anyone who's ever dabbled in key event handling knows that the ENTER key is the toughest one to implement. For one thing, it's important to cancel it because its default action is to submit the page's contents to the server, much like a form submit button. Unless you take steps to prevent the default behavior, the page will be gone before you can do much of anything! It also just happens to involve the most processing of all our key handlers.

Add the following code to the handleEnterAndTabKeys() function. Remember that we can't put it in handleSpecialKeys() because the keyup event fires after the key's default action has executed:

While we were capturing events in the last function, this time, our goal is to fire one. If you recall, the <tr> row node's click event calls an anonymous function to look up a fund's information based on its symbol. You can find it in the appendFund() function:

Let's update the click event handler by using our new addEventHandler() function, just to be sure that we don't overwrite another handler:

Getting back to the ENTER key handler, depending on which method the node supports, either the fireEvent() or the dispatchEvent() function is called. Calling fireEvent() is easy. All you need to do is pass it the event name, preceded by the 'on' prefix - onclick in this case. Browsers that support the DOM Level 2 event propagation model require a bit more setting up. The first thing is stop the default behavior from occurring, which, in this instance, would be to submit the form's contents to the server. The net result of that action is that the completeField's contents are used to perform a fund search, so instead of getting one fund back, you get details for the entire list. To stop a key's default behavior from occurring, we call the preventDefault() method on the event. The next step is to create an event of type 'MouseEvents' using document.createEvent( String eventType ). For the full list of event types, see the references at the end of the article. We need to call initMouseEvent() to initialize the event we just created. It takes quite a few parameters:

Here's an explanation of each parameter:

That's it for key event capturing. Before we call it a day, let's tie up a couple of loose ends.


[previous] [next]

Recent Articles

WebReference.com site name
Rolling Out Your Own HTML Application Version Control
HTML 5: Client-side Storage
Working with Ajax Server Extensions
internet.com site name
Wi-Fi Product Watch, November 2009
Chip Market Recovering From '08 Collapse
Low-Cost Tools to Kickstart Your New Business


internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs