Intro to jQuery
Web Systems
What is jQuery?
• A JavaScript library
• Lightweight (about 31KB for the minified version)
• Simplifies HTML document traversing (DOM), event handling, animations, and more
<aside>Minification
• Removal of all unnecessary characters in code
o e.g. whitespace new line chars, and comments
• Reduces amount of data needed to be transfered
o Smaller file size = quicker page loads, but less readability
• A lot of tools that compress the source code for you
How To Add jQuery
• Lastest stable version is 1.7.1 • Download it and store locally
o <script type="text/javascript" src="jquery.js"></script>
• Use the hosted jQuery library
o <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jque ry.min.js"></script> o <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.7.1.min.js"></script>
jQuery Syntax
• $(selector).action();
o $ (typically) used to define jQuery
o Selector - HTML element to "query" or find o Action - What to do jQuery action to perform
<aside> Defining jQuery
• $ is shorthand for the standard function (full name is jQuery)
o $(document).ready = jQuery(document).ready o Syntactically the same
• Problem: '$' is used as shorthand for other JavaScript library objects
• There's a way to get around this:
o jQuery noConflict() method
o Ex. var jq = jQuery().noConflict();
jq(document).ready( function () { jq("p").hide();
Event Handlers
jQuery methods called when an event is "triggered" or "fired" • It's common to put most jQuery functions within $
(document).ready(function)
o This waits until the entire page is loaded
• $(document).ready(function() { $("p.change").click(function() { //do something here
}); });
jQuery Selectors
• Allow you to manipulate/traverse HTML DOM element • There are 3 types of jQuery Selectors
Element Selectors
jQuery uses CSS to select HTML elements • $("h1") - selects all <h1> elements
• $("p.fname") - selects all <p> with the class = "fname" • $("h2#lname") - selects the <td> with the id = "lname" • $("#contact") - selects all elements with id = "contact"
o (There should only be one!)
Attribute Selectors
jQuery uses XPath to select elements with the given attributes • $("[href]") - selects all elements with the href attribute
• $("[name = 'fname']") - selects all elements where the name attribute is equal to "fname"
• $("[href !='#']") - selects all elements with href attribute does NOT equal "#"
CSS Selectors
Changes the CSS properties of the HTML elements • $(selector).css("css-property", "value");
o Can pass just the property to get the current value (the
first matched element is returned)
o Can pass multiple properties
• $("h1").css("color", "green") - changes the color of all h1 elements to green
• $("h1").css({"background-color":"yellow","font-size":"200%"}) - changes all h1 elements to have a background color yellow and font size to 200%
jQuery HTML Manipulation
• jQuery gives you some methods to manipulate the DOM • .addClass() - lets you add one or more classes to the
element
• .html() - sets or returns the content (innerHTML) of a element
• .before() - adds content before the given element
• .val() - sets or gets the value of a (form) element
jQuery HTML Manipulation Cont.
Ok cool, but what about this:
• You dynamically add a new element (via jQuery or some other method) and want to bind an event to it.
• You can use the .on() method
o $(parent-element-to-monitor).on("event(s)", "
element-to-attach-event", eventHandler());
o Ex.
$(document).on( "click", "p", function(event) { alert("Cool text here!");
});
jQuery Effects
The "old" way to do hide, show, slide, toggle, fade, and animate.
(A lot of this can be done with CSS3 now).
• $("p#hideme").hide() - hides the p element with the id • $("h1").fadeIn() - does a fade in animation to all h1
jQuery Effects (Callbacks) Cont.
• The callback parameter
• Waits to execute a function until after the parent function is executed
• Useful since JavaScript is a interpreted language
jQuery Effects (Callbacks) Cont.
What's the difference between these two functions? • $("p").hide(1000);
alert("The paragraph is now hidden"); • $("p").hide(1000,function(){
alert("The paragraph is now hidden"); });
jQuery UI
• Official jQuery user interface library
o Basically a set of useful/"official" jQuery plugins
• Convient UI interactions • Useful widgets
• Cool effects
Using jQuery UI
• Lastest stable version is 1.8.17
o http://jqueryui.com/download
o Lets you download only things you want
Some Basic UI Interactions
• Draggable - lets you make any DOM elements draggable • Resizable - lets you resize any DOM element
• Selectable - makes a DOM element, or group of elements, selectable
Useful Widgets
• Datepicker - A highly configurable UI datepicker
• Autocomplete - Allows a Google Search like autocomplete function
• Button - Makes making things that aren't typically buttons be buttons
• Tabs - Used to break content into different sections
• Dialog - Similar to JS alert, but more configureable
jQuery UI Effects
Basically some convenience methods that extend the functionality of jQuery
• Animate - extends the core jQuery animate function to animate by color
• Hide/Show - enables animation for the effects
jQuery UI Themes
• jQuery UI plugins are all styled by CSS (both the core jQuery UI style and plugin specific style)
• Makes it easy to keep the look and feel consistent
• Given the ability to create your own customized theme
Have a great weekend!
<footer>
http://droto.net/websys/jquery/examples.html for all the examples and more resources.