02.06.2013 Views

jQuery in Action - Manning Publications

jQuery in Action - Manning Publications

jQuery in Action - Manning Publications

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.

4 CHAPTER 1 Introduc<strong>in</strong>g <strong>jQuery</strong><br />

and scores of other prom<strong>in</strong>ent companies. Microsoft has even elected to distribute<br />

<strong>jQuery</strong> with its Visual Studio tool, and Nokia uses <strong>jQuery</strong> on all its phones that <strong>in</strong>clude<br />

their Web Runtime component.<br />

Those are not shabby credentials!<br />

Compared with other toolkits that focus heavily on clever JavaScript techniques,<br />

<strong>jQuery</strong> aims to change the way that web developers th<strong>in</strong>k about creat<strong>in</strong>g rich functionality<br />

<strong>in</strong> their pages. Rather than spend<strong>in</strong>g time juggl<strong>in</strong>g the complexities of advanced<br />

JavaScript, designers can leverage their exist<strong>in</strong>g knowledge of Cascad<strong>in</strong>g Style Sheets<br />

(CSS), Hypertext Markup Language (HTML), Extensible Hypertext Markup Language<br />

(XHTML), and good old straightforward JavaScript to manipulate page elements<br />

directly, mak<strong>in</strong>g rapid development a reality.<br />

In this book, we’re go<strong>in</strong>g to take an <strong>in</strong>-depth look at what <strong>jQuery</strong> has to offer us as<br />

developers of highly <strong>in</strong>teractive web applications. Let’s start by f<strong>in</strong>d<strong>in</strong>g out exactly<br />

what <strong>jQuery</strong> br<strong>in</strong>gs to the web development party.<br />

You can obta<strong>in</strong> the latest version of <strong>jQuery</strong> from the <strong>jQuery</strong> site at http://<br />

jquery.com/. Install<strong>in</strong>g <strong>jQuery</strong> is as easy as plac<strong>in</strong>g it with<strong>in</strong> your web application and<br />

us<strong>in</strong>g the HTML tag to <strong>in</strong>clude it <strong>in</strong> your pages, like this:<br />

<br />

The specific version of <strong>jQuery</strong> that the code <strong>in</strong> this book was tested aga<strong>in</strong>st is <strong>in</strong>cluded<br />

as part of the downloadable code examples (available at http://www.mann<strong>in</strong>g.com/<br />

bibeault2).<br />

1.1 Power <strong>in</strong> the economy of code<br />

If you’ve spent any time at all try<strong>in</strong>g to add dynamic functionality to your pages, you’ve<br />

found that you’re constantly follow<strong>in</strong>g a pattern of select<strong>in</strong>g an element (or group of<br />

elements) and operat<strong>in</strong>g upon those elements <strong>in</strong> some fashion. You could be hid<strong>in</strong>g<br />

or reveal<strong>in</strong>g the elements, add<strong>in</strong>g a CSS class to them, animat<strong>in</strong>g them, or <strong>in</strong>spect<strong>in</strong>g<br />

their attributes.<br />

Us<strong>in</strong>g raw JavaScript can result <strong>in</strong> dozens of l<strong>in</strong>es of code for each of these tasks,<br />

and the creators of <strong>jQuery</strong> specifically created that library to make common tasks trivial.<br />

For example, anyone who has dealt with radio groups <strong>in</strong> JavaScript knows that it’s<br />

a lesson <strong>in</strong> tedium to discover which radio element of a radio group is currently<br />

checked and to obta<strong>in</strong> its value attribute. The radio group needs to be located, and<br />

the result<strong>in</strong>g array of radio elements must be <strong>in</strong>spected, one by one, to f<strong>in</strong>d out which<br />

element has its checked attribute set. This element’s value attribute can then be<br />

obta<strong>in</strong>ed.<br />

Such code might be implemented as follows:<br />

var checkedValue;<br />

var elements = document.getElementsByTagName('<strong>in</strong>put');<br />

for (var n = 0; n < elements.length; n++) {<br />

if (elements[n].type == 'radio' &&<br />

elements[n].name == 'someRadioGroup' &&

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

Saved successfully!

Ooh no, something went wrong!