More jQuery


var onButtonClick = function() {

$('button').on('click', onButtonClick);

$('button').on('click', function () {


Follow along:

Remember: Callbacks


Arguments get applied in callbacks

var myCallback = function(event) {

$('p').on('mouseenter', myCallback);

Preventing Default Events

$('a').on('click', function (event) {
	console.log('Not going there!');
$('form').on('submit', function (event) {
	console.log('Not submitting, time to validate!');

Effects & Animation



$('#error').show(1000, function(){

Exercise Time!

click here!

jQuery Plugins

"If you want to create an animation, effect, or UI component, chances are pretty good that someone has done the work for you already."

Which plugin should you pick?

jQuery Plugin Usage

  • Download the plugin and associated files from the site or github repo.
  • Copy them into your project folder.
  • In the HTML, reference any associated CSS files.
  • <link rel="type/stylesheet" type="text/css" href="tablesorter.css">
  • In the HTML, add a <script> tag for the jQuery plugin itself.
  • <script src="lib/tablesorter.js"><script>
  • In the JavaScript, call the jQuery plugin on your DOM.
  • $('table').tableSorter();

Exercise Time!

click here!

jQuery Patterns and Anti-Patterns

Pattern: name variables with $

var $myVar = $('#myNode');

Pattern: bind events to the document

$(document).on('click', 'a', myCallback);

Storing References

Pattern: store references to callback functions

var myCallback = function(argument) {
  // do something cool

$(document).on('click', 'p', myCallback);

Anti-pattern: anonymous functions

$(document).on('click', 'p', function(argument) {
  // do something anonymous


banner.css('color', 'red');

Is the same as:

banner.css('color', 'red').html('Welcome!').show();

Is the same as:

banner.css('color', 'red')

DOM Readiness

Broken code: DOM manipulation and event binding in the <head>

    $('body').append( myNode );

Pattern: If you need to load a script in the <head>, wait for the rest of the DOM to be ready before executing

    $(document).ready(function() {
      // do something when the DOM is fully loaded

jQuery great.

But do not become a jQuery-dependant!