Introduction to jQuery (for Drupal) Amit Asaravala [email protected] “aasarava” on...

32
Introduction to jQuery (for Drupal) Amit Asaravala http://returncontrol.com/badcamp [email protected] “aasarava” on drupal.org

Transcript of Introduction to jQuery (for Drupal) Amit Asaravala [email protected] “aasarava” on...

Page 1: Introduction to jQuery (for Drupal) Amit Asaravala  amit@returncontrol.com “aasarava” on drupal.org.

Introduction to jQuery(for Drupal)

Amit Asaravalahttp://returncontrol.com/badcamp

[email protected]“aasarava” on drupal.org

Page 2: Introduction to jQuery (for Drupal) Amit Asaravala  amit@returncontrol.com “aasarava” on drupal.org.

What is jQuery?

Page 3: Introduction to jQuery (for Drupal) Amit Asaravala  amit@returncontrol.com “aasarava” on drupal.org.

What is jQuery?

•Magic

Page 4: Introduction to jQuery (for Drupal) Amit Asaravala  amit@returncontrol.com “aasarava” on drupal.org.

What is jQuery?

• A JavaScript library:• Makes writing JavaScript a lot easier• Makes writing cross-browser compatible

JavaScript a lot easier• Makes creating client-side interactivity a lot easier• Makes AJAX a lot easier

Page 5: Introduction to jQuery (for Drupal) Amit Asaravala  amit@returncontrol.com “aasarava” on drupal.org.

How Do I Get jQuery?

Page 6: Introduction to jQuery (for Drupal) Amit Asaravala  amit@returncontrol.com “aasarava” on drupal.org.

How Do I Get jQuery?

• For Drupal Sites:– D6 jQuery 1.2.6 comes bundled– D7 jQuery 1.4.4 comes bundled – Loaded automatically– Can use jquery_update module for newer vers.

• For Non-Drupal Sites:– http://jquery.com/download– Get the “minified” version for production use

Page 7: Introduction to jQuery (for Drupal) Amit Asaravala  amit@returncontrol.com “aasarava” on drupal.org.

How Do I Use It?

Page 8: Introduction to jQuery (for Drupal) Amit Asaravala  amit@returncontrol.com “aasarava” on drupal.org.

How Do I Use It: Non-Drupal

<head> <script type=“javascript” src=“jquery.js”></script> <script type=“javascript”> //your jquery code goes here. go crazy! </script></head>

Page 9: Introduction to jQuery (for Drupal) Amit Asaravala  amit@returncontrol.com “aasarava” on drupal.org.

How Do I Use It: Drupal Theme

• Drupal 6:– Create a script.js file in your theme folder.– Your jQuery code goes in there.

• Drupal 7: – Edit your theme’s .info file– Add: scripts[] = script.js

• Reference: http://drupal.org/node/171213

Page 10: Introduction to jQuery (for Drupal) Amit Asaravala  amit@returncontrol.com “aasarava” on drupal.org.

How Do I Use It: Drupal Module

1. Create a mymodule.js file.2. Use drupal_add_js() function in your module.

mymodule_init() { $path = drupal_get_path(‘module’, ‘mymodule’); drupal_add_js($path . '/mymodule.js');}

Page 11: Introduction to jQuery (for Drupal) Amit Asaravala  amit@returncontrol.com “aasarava” on drupal.org.

Yeah, But How Do I Use It?

Page 12: Introduction to jQuery (for Drupal) Amit Asaravala  amit@returncontrol.com “aasarava” on drupal.org.

Writing jQuery

1. Series of statements2. Each statement is usually a 3-step process:

1. Select an element2. Attach an event (optional)3. Specify what happens

“For this heading,when the user clicks on it,I want monkeys to fly across the page.”

Page 13: Introduction to jQuery (for Drupal) Amit Asaravala  amit@returncontrol.com “aasarava” on drupal.org.

3-Step Process

1. Select element:jQuery( ‘h2’ )

2. Attach an event:jQuery( ‘h2’ ).click( )

3. Specify what happens:jQuery( ‘h2’ ).click( monkeysFly );

Page 14: Introduction to jQuery (for Drupal) Amit Asaravala  amit@returncontrol.com “aasarava” on drupal.org.

Your Final Statement

jQuery(‘h2’).click( monkeysFly );

Page 15: Introduction to jQuery (for Drupal) Amit Asaravala  amit@returncontrol.com “aasarava” on drupal.org.

$horthand

$(‘h2’).click( monkeysFly );

see example

Page 16: Introduction to jQuery (for Drupal) Amit Asaravala  amit@returncontrol.com “aasarava” on drupal.org.

More Selectors

$(‘input’).click( monkeysFly );$(‘.title’).click( monkeysFly );

$(‘#form1 input’).click( monkeysFly );$(‘#edit-title’).click( monkeysFly );

$(‘h2, h3, .title’).click( monkeysFly );

Page 17: Introduction to jQuery (for Drupal) Amit Asaravala  amit@returncontrol.com “aasarava” on drupal.org.

Monkey Code

Inside script.js:

$('h2').click( monkeysFly );

function monkeysFly() { $('img.monkey').fadeIn('slow');}

Page 18: Introduction to jQuery (for Drupal) Amit Asaravala  amit@returncontrol.com “aasarava” on drupal.org.

Anonymous Functions

$('h2').click( function() { $('img.monkey').fadeIn('slow'); });

Page 19: Introduction to jQuery (for Drupal) Amit Asaravala  amit@returncontrol.com “aasarava” on drupal.org.

Anonymous Functions, 2

$('h2').click(function() { $('img.monkey').fadeIn('slow'); });

Page 20: Introduction to jQuery (for Drupal) Amit Asaravala  amit@returncontrol.com “aasarava” on drupal.org.

Wait ‘Til Browser is Ready

$(document).ready(function() {

$('h2').click(function() { $('img.monkey').fadeIn('slow'); });

});

Page 21: Introduction to jQuery (for Drupal) Amit Asaravala  amit@returncontrol.com “aasarava” on drupal.org.

D6: Wait ‘Til Browser is Ready

Drupal.behaviors.demo = function(context) {

$('h2‘, context).click(function() { $('img.monkey').fadeIn('slow'); });

};

Page 22: Introduction to jQuery (for Drupal) Amit Asaravala  amit@returncontrol.com “aasarava” on drupal.org.

D7: Wait ‘Til Browser is Ready

Drupal.behaviors.demo = { attach: function(context, settings) { $('h2‘, context).click(function() { $('img.monkey').fadeIn('slow'); }); }};

Page 23: Introduction to jQuery (for Drupal) Amit Asaravala  amit@returncontrol.com “aasarava” on drupal.org.

Finding the Right jQuery Function

• Official: http://api.jquery.com/• Handy: http://jqapi.com/

Page 24: Introduction to jQuery (for Drupal) Amit Asaravala  amit@returncontrol.com “aasarava” on drupal.org.

Example: Dynamic Form Components

• Show character count as user is typing?No problem!

Page 25: Introduction to jQuery (for Drupal) Amit Asaravala  amit@returncontrol.com “aasarava” on drupal.org.

Example: Dynamic Form Components

Let’s do this in a modulecountchars.module:

function countchars_init() {$path = drupal_get_path(‘module’, ‘countchars’);drupal_add_js( $path . ‘/countchars.js’);

}

Page 26: Introduction to jQuery (for Drupal) Amit Asaravala  amit@returncontrol.com “aasarava” on drupal.org.

Example: Dynamic Form Components

countchars.js

$(document).ready(function() {

});

Page 27: Introduction to jQuery (for Drupal) Amit Asaravala  amit@returncontrol.com “aasarava” on drupal.org.

countchars.js

$(document).ready(function() { $('#mytext').keyup( function() {

});});

Page 28: Introduction to jQuery (for Drupal) Amit Asaravala  amit@returncontrol.com “aasarava” on drupal.org.

countchars.js

$(document).ready(function() { $('#mytext').after( '<div id="chars">characters: 0</div>' );

$('#mytext').keyup( function() { });});

Page 29: Introduction to jQuery (for Drupal) Amit Asaravala  amit@returncontrol.com “aasarava” on drupal.org.

Chaining

$(document).ready(function() { $('#mytext') .after( '<div id="chars">characters: 0</div>' ) .keyup( function() {

});});

Page 30: Introduction to jQuery (for Drupal) Amit Asaravala  amit@returncontrol.com “aasarava” on drupal.org.

countchars.js

$(document).ready(function() { $('#mytext') .after( '<div id="chars">characters: 0</div>' ) .keyup(function() {

var count = $('#mytext').val().length; $('#chars').html( 'characters: ' + count );

});});

Page 31: Introduction to jQuery (for Drupal) Amit Asaravala  amit@returncontrol.com “aasarava” on drupal.org.

$(this)

$(document).ready(function() { $('#mytext') .after( '<div id="chars">characters: 0</div>' ) .keyup(function() {

var count = $(this).val().length; $('#chars').html( 'characters: ' + count );

});});

Page 32: Introduction to jQuery (for Drupal) Amit Asaravala  amit@returncontrol.com “aasarava” on drupal.org.

Resources• jquery.com -- main site• jqueryui.com -- UI components

[email protected]

http://returncontrol.com/badcamp