How to Use jQuery
to Enhance your Web Design

Future of Web Design, 2009

* Right arrow key or Return/Enter to move forward; left arrow key to move backward. Or, use slide list at bottom of page (appears on hover)

Who Am I?

Who Are You?

JavaScript

A Very Brief (and Opinionated) History

Inline Scripts

<a href="#whatisit" onMouseOut="MM_swapImgRestore()"
  onMouseOver="MM_swapImage('Whatis','',
  'whatis-active.png',1)">
  <img name="Whatis" src="buttons/whatis-unactive.png"></a>
		

Inline Scripts

<ul>
  <script>if(DetermineFocus('contactus.htm')) {
    document.write('<li class="selected">')
  } else {
    document.write('<li>')
  }</script>

DOM Scripting /
Web Standards

ppk on javascript DOM Scripting

Unobtrusive

Tests Features

var x,y;
if (self.innerHeight) { // all except Explorer
  x = self.innerWidth;
  y = self.innerHeight;
}
else if (document.documentElement && 
  document.documentElement.clientHeight) { 
   // Explorer 6 Strict Mode
  x = document.documentElement.clientWidth;
  y = document.documentElement.clientHeight;
}
else if (document.body) { // other Explorers
  x = document.body.clientWidth;
  y = document.body.clientHeight;
}

Along Came jQuery

(and other libraries)

jQuery Is Open Source

jQuery Is Open Source

Copyright (c) 2009 John Resig, http://jquery.com/

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND …

jQuery Is Unobtrusive

jQuery Has Quick, Terse Syntax

var x = $(window).width();
var y = $(window).height();

jQuery Simplifies Interaction

Find Some Elements

$

$ == jQuery

$( )

$() == jQuery()

$( 'div' )

Do Something With Them

Let elements "listen" for something to happen…

Do Something With Them

…and then do something

Reference Scripts

Download jQuery …

… or Use a CDN

Reference Scripts

<head>
  <!-- etc. -->
  <link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>  
  <!-- etc. -->
  <script src="/scripts/jquery.js"></script>
  <script src="/scripts/my-custom-script.js"></script>
</body>

Reference Scripts

<head>
  <!-- etc. -->
  <link rel="stylesheet" href="style.css" type="text/css" />
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
  <script src="/scripts/my-custom-script.js"></script>
</head>

Reference Scripts

<head>
  <!-- etc. -->
  <link rel="stylesheet" href="style.css" type="text/css" />
  <script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.3.2.min.js"></script>
</head>
<body>  
  <!-- etc. -->
  <script src="/scripts/my-custom-script.js"></script>
</body>

$(document).ready( )

$(document).ready( )

$(document).ready(function() {	
	// Stuff to do as soon as the DOM is ready;
});

After $(document).ready( ) ?

$(window).bind('load', function() {	
	// Stuff to do after everything is loaded;
});

Before $(document).ready( ) ?

<head>
  <script type="text/javascript">
    document.documentElement.className = 'js';
  </script>
  <!-- etc. -->          
</head>

Script Placement and Execution

Back to Finding Something

CSS Selectors

CSS Selectors (with jQuery)

More CSS Selectors

More CSS Selectors (with jQuery)

More Selectors (with jQuery)

Selector Demo

  • Selector:
<div class="body">

<h2>Some Header</h2>

<div class="contents">

<p>Paragraph 1</p>

<p>Paragraph 2</p>

<p>Paragraph 3</p>

</div>
</div>

Some Header

Paragraph 1

Paragraph 2

Paragraph 3

* Because of the crazy way we're doing this, a few selectors (such as nth-child) will produce unexpected results in the code view box (on the left).

Traversing

Let's Do Something!

$('li:lt(2)').addClass('special');

Manipulation - .after( )

$('a[href$=.pdf]')
  .after(' <img src="/images/pdf.png" alt="PDF" />');

Manipulation - .css( )

$('a').css({
  color: 'red',
  fontWeight: 'bold',
  'background-color': '#ff3'
});

Interaction - .submit( )

$('form').submit(function() {
  if ( $('#name').val() == '' ) {
    $('span.help').show();
      return false;
  }
});
  • Name:

Interaction - .click( )

$('a.menu').click(function() {
  $(this).next().toggle();
  return false;
});

Animation - .slideToggle( )

$('a.menu').click(function() {
  $(this).next().slideToggle('slow');
  return false;
});

Animation - .animate( )

$('div.block').animate({
  fontSize: '2em',
  width: '+=20%',
  backgroundColor: 'green'
});
hello!
* Requires the jQuery color plugin

Animation with a “Callback”

$('div.block').hide('slow', function() {
  $(this).show('slow');
});
hello!

Ajax - .load( )

$('div.load').load('file.html');
hello!

Ajax - .load( ) with Selector

$('div.load').load('file.html h2');
hello!

“Live” Events

$('tr')
  .bind('click', function() {
    $(this).css({color: red});
  })
  .live('click', function() {
    $(this).css({backgroundColor: yellow});
  });
heading another yet another
data more data even more
data more data even more

Chaining

$('div').css('color','blue');
$('div').css('color','blue').addClass('hehe');
$('div')
  .css('color','blue')
  .addClass('hehe')
  .append('<p>bye</p>');

HTML Selector

$('<li><a></a></li>')
.find('a')
  .attr('href', 'http://www.learningjquery.com/')
  .html('Learn More')
.end()
.appendTo('#html ul');

Simple Row Striping

$(' ').addClass('alt');

heading another yet another
data more data even more
data more data even more
data more data even more
heading another yet another
data more data even more
data more data even more

Plugins

Apply a Plugin

$(document).ready(function() {
  $('#slideshow').cycle();
});

Plugin Options

$(document).ready(function() {
  $('#slideshow').cycle({timeout: 8000, pause: true});
});

Plugin Options

$('#slideshow').cycle(
{ 
  fx: 'scrollHorz',
  timeout: 8000, 
  pause: true 
}
);

jQuery UI

jqueryui.com

jQuery UI

ThemeRoller

A Few Recommended Plugins

from Mike Alsup: malsup.com/jquery

A Few Recommended Plugins

from Brandon Aaron github.com/brandonaaron

A Few Recommended Plugins

from me plugins.learningjquery.com, github.com/kswedberg

A Few Recommended Plugins

from others

Let's Take a Look

Thank You!