06.05.2013 Views

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

[ 297 ]<br />

Chapter 9<br />

Adding this class serves two purposes. First, it makes sure that the same code<br />

cannot be run on it again (not a likely event, given that we run the code in the ready<br />

h<strong>and</strong>ler). Second, it identifies that element as an auto<strong>com</strong>plete form. The <strong>Drupal</strong><br />

CSS then styles the element accordingly, adding the st<strong>and</strong>ard auto<strong>com</strong>plete throbber<br />

inside the text box:<br />

Note the grey circle near the end of the search box. That is the location of the<br />

throbber icon. When the auto<strong>com</strong>pletion AJAX script is running, the throbber icon<br />

will be displayed as a spinning circle.<br />

Let's now turn to the anonymous function that is run inside the each() function:<br />

$('input[name="search_block_form"]:not(.form-auto<strong>com</strong>plete)')<br />

.each(function () {<br />

var newId = $(this).attr('id') + '-auto<strong>com</strong>plete';<br />

var newElement = $('')<br />

.addClass('auto<strong>com</strong>plete')<br />

.attr('id', newId).attr('disabled','disabled')<br />

.attr('value', SearchAuto<strong>com</strong>plete.url);<br />

$(this).after(newElement);<br />

}).addClass('form-auto<strong>com</strong>plete');<br />

In a nutshell, the highlighted code creates a new hidden element that contains<br />

instructions for the auto<strong>com</strong>plete h<strong>and</strong>ler. This hidden element is then added after<br />

the search box input element.<br />

The anonymous function called by each does the following. First, it creates a new ID<br />

based on the id attribute of the current input element. This ID will be assigned to<br />

the new hidden element. The auto<strong>com</strong>plete.js library will use the ID to correlate<br />

the new hidden element with the field that it describes. So the naming convention<br />

used (original ID plus -auto<strong>com</strong>plete) is important.<br />

This material is copyright <strong>and</strong> is licensed for the sole use by Richard Ostheimer on 18th June 2009<br />

2205 hilda ave., , missoula, , 59801

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!