A brief introduction to the jQuery javascript library and the problems it helps solve. We'll review some of the basic syntax and walk through a 'hello world' example.
creates demand for complex Rich Internet Applications • jQuery abstracts away differences between browsers, permitting focus on development • Plug-in framework provides an easy method to share and use code • Most popular and widely used of JS libraries
equivalent $() is just shorthand • Accepts HTML tag id, CSS selectors or XPath • Examples: Give me all images: $('img') • Give me all items with CSS class header: $('.header')
URL, click Fork button • Make some changes, hit Run to see them • If you want to save, click Update • Oh no! If you break something, just hit reload, or decrement the URL to roll back
jQuery looks at input, if looks like HTML tag syntax, it creates this DOM Element $('img') IS NOT $('<img>') • $('img') returns all existing images • $('<img>') returns a brand new Image element
Example: Find all images and add a CSS class $('img').addClass('left-align'); • Functions can be chained $('img').addClass('left-align').fadeOut('slow');
- Hide or show a jQuery element • addClass, removeClass, toggleClass - Add, remove or toggle add/remove classes • append, appendTo, prepend, prependTo - Different ways of attaching content • attr - Set/get attributes, example img alt value
page var $my_images = $('img'); //Lets print out each image location $my_images.each(function() { var img_url = $(this).attr('src'); alert(img_url); });
already are familiar with lots: • click Example: Clicking a link • mouseover Example: Moving mouse cursor over image • focus Example: Moving between form fields
gallery JS Fiddle page http://jsfiddle.net/kristian/EEt9Z/1/ • GetJSON method queries Flickr • Returns results in JSON format • We parse this and add to our page • Finally, add a modal popup window