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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>jQuery</strong>: Do More with <strong>Drupal</strong><br />

The highest variable will store the size of the highest node. By default, we set it to<br />

100 pixels.<br />

Next, we make use of another <strong>jQuery</strong> feature. Along with the rest of the <strong>jQuery</strong><br />

goodies, there are a h<strong>and</strong>ful of basic list-traversing functions. One of these is the<br />

each() function, which iterates through each item in the list of nodes wrapped<br />

by a <strong>jQuery</strong> object. On each object, it executes the function passed into the each<br />

() function.<br />

There are actually two <strong>jQuery</strong> each() functions: One that iterates<br />

through the contents of a <strong>jQuery</strong> object, <strong>and</strong> one that iterates through<br />

any list-like object. The list-iterating version is called on the main <strong>jQuery</strong><br />

object like this: <strong>jQuery</strong>.each(myList, function () {}).<br />

So this version of each() takes one argument—a function object.<br />

In our case, we are going to supply the each() function with an anonymous function<br />

that will do the height <strong>com</strong>parison:<br />

stickies.each(function () {<br />

var stickyHeight = $(this).height();<br />

if(stickyHeight > highest) {<br />

highest = stickyHeight;<br />

}<br />

});<br />

When that anonymous function is executed, the this variable is set to the current list<br />

item. In our case, this will contain the current DOM element in the list of stickies.<br />

This method of formatting inline anonymous functions is conventional,<br />

<strong>and</strong> you will see the same formatting throughout <strong>jQuery</strong> <strong>and</strong> <strong>Drupal</strong>.<br />

It is important to note that this is a DOM element object, not a <strong>jQuery</strong> object.<br />

We want to find the height of that object, but the easiest way to get the height of<br />

an element is to use the <strong>jQuery</strong> library. So in the second line, we pass this into<br />

$() to get a new <strong>jQuery</strong> object, Then we use <strong>jQuery</strong>'s height() function to find<br />

out the element's height. This is all packed into the line that reads: var<br />

stickyHeight = $(this).height().<br />

While the first two lines of this code sample are <strong>com</strong>plex, the next part<br />

is straightforward:<br />

if(stickyHeight > highest) {<br />

highest = stickyHeight;<br />

}<br />

[ 82 ]<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!