jquery Tutorial for Beginners: Nothing But the Goods



Similar documents
The purpose of jquery is to make it much easier to use JavaScript on your website.

Example. Represent this as XML

WP Popup Magic User Guide

Web Development 1 A4 Project Description Web Architecture

WP Popup Magic User Guide

MASTERTAG DEVELOPER GUIDE

PLAYER DEVELOPER GUIDE

WEB DESIGN COURSE CONTENT

CMSC434 TUTORIAL #3 HTML CSS JavaScript Jquery Ajax + Google AppEngine Mobile WebApp HTML5

Lecture 9 Chrome Extensions

JavaScript By: A. Mousavi & P. Broomhead SERG, School of Engineering Design, Brunel University, UK

Responsive Web Design Creative License

Making Web Application using Tizen Web UI Framework. Koeun Choi

HTML Form Widgets. Review: HTML Forms. Review: CGI Programs

JQUERY - EFFECTS. Showing and Hiding elements. Syntax. Example

Course Information Course Number: IWT 1229 Course Name: Web Development and Design Foundation

^/ CS> KRIS. JAMSA, PhD, MBA. y» A- JONES & BARTLETT LEARNING

Fast track to HTML & CSS 101 (Web Design)

Interactive Data Visualization for the Web Scott Murray

Intro to jquery. Web Systems 02/17/2012

Debugging JavaScript and CSS Using Firebug. Harman Goei CSCI 571 1/27/13

BT CONTENT SHOWCASE. JOOMLA EXTENSION User guide Version 2.1. Copyright 2013 Bowthemes Inc.

Citrix StoreFront. Customizing the Receiver for Web User Interface Citrix. All rights reserved.

Mobile Web Design with HTML5, CSS3, JavaScript and JQuery Mobile Training BSP-2256 Length: 5 days Price: $ 2,895.00

BT BACKGROUND SLIDESHOW JOOMLA EXTENSION User guide Version 2.0

Web Development. Owen Sacco. ICS2205/ICS2230 Web Intelligence

Website Login Integration

SAHARA DIGITAL8 RESPONSIVE MAGENTO THEME

Dreamweaver CS3 THE MISSING MANUAL. David Sawyer McFarland. POGUE PRESS" O'REILLY 8 Beijing Cambridge Farnham Koln Paris Sebastopol Taipei Tokyo

SAHARA FASHION15 RESPONSIVE MAGENTO THEME

Web Development CSE2WD Final Examination June (a) Which organisation is primarily responsible for HTML, CSS and DOM standards?

Using Style Sheets for Consistency

ICE: HTML, CSS, and Validation

CLASSROOM WEB DESIGNING COURSE

Garfield Public Schools Fine & Practical Arts Curriculum Web Design

Microsoft Expression Web Quickstart Guide

Using Spry Widgets. In This Chapter

Intro to Web Design. ACM UIUC

Slide.Show Quick Start Guide

Tutorial JavaScript: Switching panels using a radio button

If you, God forbid, find a bug, let me know and I ll try to fix it as soon as I can, ok?

WEB DEVELOPMENT COURSE (PHP/ MYSQL)

Web Development Recipes

CREATING A NEWSLETTER IN ADOBE DREAMWEAVER CS5 (step-by-step directions)

Website Builder Documentation

Module 6 Web Page Concept and Design: Getting a Web Page Up and Running

10CS73:Web Programming

Fortis Theme. User Guide. v Magento theme by Infortis. Copyright 2012 Infortis

Dreamweaver CS4 Day 2 Creating a Website using Div Tags, CSS, and Templates

Last week we talked about creating your own tags: div tags and span tags. A div tag goes around other tags, e.g.,:

Introduction to web development using XHTML and CSS. Lars Larsson. Today. Course introduction and information XHTML. CSS crash course.

Website Planning Checklist

Advanced Web Development SCOPE OF WEB DEVELOPMENT INDUSTRY

How To Change Your Site On Drupal Cloud On A Pcode On A Microsoft Powerstone On A Macbook Or Ipad (For Free) On A Freebie (For A Free Download) On An Ipad Or Ipa (For

Web Developer Jr - Newbie Course

Using jquery and CSS to Gain Easy Wins in CiviCRM

Creating Web Pages with HTML Simplified. 3rd Edition

Magento 1.4 Theming Cookbook

Interspire Website Publisher Developer Documentation. Template Customization Guide

Advantage of Jquery: T his file is downloaded from

Client-side Web Engineering From HTML to AJAX

Dashboard Skin Tutorial. For ETS2 HTML5 Mobile Dashboard v3.0.2

BASICS OF WEB DESIGN CHAPTER 2 HTML BASICS KEY CONCEPTS COPYRIGHT 2013 TERRY ANN MORRIS, ED.D

Skills for Employment Investment Project (SEIP)

Website Development Komodo Editor and HTML Intro

User Guide for Smart Former Gold (v. 1.0) by IToris Inc. team

Web Design Basics. Cindy Royal, Ph.D. Associate Professor Texas State University

AJAX with jquery. ACM Webmonkeys 2011

<?xml version= 1.0?> <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN

CIS 467/602-01: Data Visualization

WHITEPAPER. Skinning Guide. Let s chat Velaro info@velaro.com by Velaro

Team Members: Christopher Copper Philip Eittreim Jeremiah Jekich Andrew Reisdorph. Client: Brian Krzys

Lab 2: Visualization with d3.js

MS Word 2007 practical notes

Subject Tool Remarks What is JQuery. Slide Javascript Library

Web Design I. Spring 2009 Kevin Cole Gallaudet University

CREATING HORIZONTAL NAVIGATION BAR USING ADOBE DREAMWEAVER CS5

Version 4.0. Unit 3: Web Design. Computer Science Equity Alliance, Exploring Computer Science Unit 3: Web Design 102

Using Adobe Dreamweaver CS4 (10.0)

ANIMATED HEADER IMAGE WITH IMAGE HEADER SLIDESHOW (FL_HEADER_SLIDE)

Joomla! template Blendvision v 1.0 Customization Manual

Web Development I & II*

Facebook Twitter YouTube Google Plus Website . o Zooming and Panning. Panel. 3D commands. o Working with Canvas

Performance Testing for Ajax Applications

Web Design for Programmers. Brian Hogan NAPCS Slides and content 2008 Brian P. Hogan Do not reproduce in any form without permission

Tutorial: Building a Dojo Application using IBM Rational Application Developer Loan Payment Calculator

ACE: Dreamweaver CC Exam Guide

How to Add a Transparent Flash FLV movie to your Web Page Tutorial by R. Berdan Oct

A send-a-friend application with ASP Smart Mailer

Transcription:

jquery Tutorial for Beginners: Nothing But the Goods Not too long ago I wrote an article for Six Revisions called Getting Started with jquery that covered some important things (concept-wise) that beginning jquery developers should know. This article is the complete opposite; there s no concepts, no principles, and very little lecturing -- just some straight example code with brief descriptions demonstrating what you can do with jquery. This fast-paced tutorial should be able to get beginners up and running with jquery very quickly, while also providing a good overview of what jquery is capable of (although jquery s capabilities go far beyond this beginning tutorial). Keep in mind that this tutorial is just a bunch of straightforward, superficial code examples and very brief explanations for beginners who want to avoid all the jargon and complexities. But I still highly recommend that all beginners get past this stuff by means of a good book, some more in-depth tutorials online, or by using the jquery documentation. Link to jquery's Source Code Remotely This is an optional technique that many developers are using today. Instead of hosting the jquery source code on your own server, you can link to it remotely. This ensures the fastest possible download time of the source code, since many users visiting your site might have the file already cached in their browser. Here is how your <script> tag should look: <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js?ver=1.4.0"></scri pt> Running Code Unobtrusively When the DOM is Ready The first thing you need to be up and running with jquery is what s called the document ready handler. Pretty much everything you code in jquery will be contained inside one of these. This accomplishes two things: First, it ensures that the code does not run until the DOM is ready. This confirms that any elements being accessed are actually in existence, so the script won t return any errors. Second, this ensures that your code is unobtrusive. That is, it s separated from content (XHTML) and presentation (CSS). Here is what it looks like: $(document).ready(function() { // all jquery code goes here ); Although you will normally include your jquery code inside the above handler, for brevity the rest of the code examples in this tutorial will not include the ready handler.

Selecting Elements in jquery The jquery library allows you to select elements in your XHTML by wrapping them in $("") (you could also use single quotes), which is the jquery wrapper. Here are some examples of wrapped sets in jquery: $("div"); // selects all HTML div elements $("#myelement"); // selects one HTML element with ID "myelement" $(".myclass"); // selects HTML elements with class "myclass" $("p#myelement"); // selects HTML paragraph element with ID "myelement" $("ul li a.navigation"); // selects anchors with class "navigation" that are nested in list items jquery supports the use of all CSS selectors, even those in CSS3. Here are some examples of alternate selectors: $("p > a"); // selects anchors that are direct children of paragraphs $("input[type=text]"); // selects inputs that have specified type $("a:first"); // selects the first anchor on the page $("p:odd"); // selects all odd numbered paragraphs $("li:first-child"); // selects each list item that's the first child in its list jquery also allows the use of its own custom selectors. Here are some examples: $(":animated"); // selects elements currently being animated $(":button"); // selects any button elements (inputs or buttons) $(":radio"); // selects radio buttons $(":checkbox"); // selects checkboxes $(":checked"); // selects checkboxes or radio buttons that are selected $(":header"); // selects header elements (h1, h2, h3, etc.) Manipulating and Accessing CSS Class Names jquery allows you to easily add, remove, and toggle CSS classes, which comes in handy for a variety of practical uses. Here are the different syntaxes for accomplishing this: $("div").addclass("content"); // adds class "content" to all <div> elements $("div").removeclass("content"); // removes class "content" from all <div> elements $("div").toggleclass("content"); // toggles the class "content" on all <div> elements (adds it if it doesn't exist, // and removes it if it does) You can also check to see if a selected element has a particular CSS class, and then run some code if it does. You would check this using an if statement. Here is an example: if ($("#myelement").hasclass("content")) { // do something here You could also check a set of elements (instead of just one), and the result would return "true" if any one of the elements contained the class.

Manipulating CSS Styles with jquery CSS styles can be added to elements easily using jquery, and it's done in a cross-browser fashion. Here are some examples to demonstrate this: $("p").css("width", "400px"); // adds a width to all paragraphs $("#myelement").css("color", "blue") // makes text color blue on element #myelement $("ul").css("border", "solid 1px #ccc") // adds a border to all lists Adding, Removing, and Appending Elements and Content There are a number of ways to manipulate groups of elements with jquery, including manipulating the content of those elements (whether text, inline elements, etc). Get the HTML of any element (similar to innerhtml in JavaScript): var myelementhtml = $("#myelement").html(); // variable contains all HTML (including text) inside #myelement If you don't want to access the HTML, but only want the text of an element: var myelementhtml = $("#myelement").text(); // variable contains all text (excluding HTML) inside #myelement Using similar syntax to the above two examples, you can change the HTML or text content of a specified element: $("#myelement").html("<p>this is the new content.</p>"); // content inside #myelement will be replaced with that specified $("#myelement").text("this is the new content."); // text content will be replaced with that specified To append content to an element: $("#myelement").append("<p>this is the new content.</p>"); // keeps content intact, and adds the new content to the end $("p").append("<p>this is the new content.</p>"); // add the same content to all paragraphs jquery also offers use of the commands appendto(), prepend(), prependto(), before(), insertbefore(), after(), and insertafter(), which work similarly to append() but with their own unique characteristics that go beyond the scope of this simple tutorial. Dealing with Events in jquery Specific event handlers can be established using the following code: $("a").click(function() { // do something here // when any anchor is clicked ); The code inside function() will only run when an anchor is clicked. Some other common events you might use in jquery include: blur, focus, hover, keydown, load, mousemove, resize, scroll, submit, select.

Showing and Hiding Elements with jquery The syntax for showing, hiding an element (or toggling show/hide) is: $("#myelement").hide("slow", function() { // do something once the element is hidden $("#myelement").show("fast", function() { // do something once the element is shown $("#myelement").toggle(1000, function() { // do something once the element is shown/hidden Remember that the "toggle" command will change whatever state the element currently has, and the parameters are both optional. The first parameter indicates the speed of the showing/hiding. If no speed is set, it will occur instantly, with no animation. A number for "speed" represents the speed in milliseconds. The second parameter is an optional function that will run when the command is finished executing. You can also specifically choose to fade an element in or out, which is always done by animation: $("#myelement").fadeout("slow", function() { // do something when fade out finished $("#myelement").fadein("fast", function() { // do something when fade in finished To fade an element only partially, either in or out: $("#myelement").fadeto(2000, 0.4, function() { // do something when fade is finished The second parameter (0.4) represents "opacity", and is similar to the way opacity is set in CSS. Whatever the opacity is to start with, it will animate (fadeto) until it reaches the setting specified, at the speed set (2000 milliseconds). The optional function (called a "callback function") will run when the opacity change is complete. This is the way virtually all callback functions in jquery work.

jquery Animations and Effects You can slide elements, animate elements, and even stop animations in mid-sequence. To slide elements up or down: $("#myelement").slidedown("fast", function() { // do something when slide down is finished $("#myelement").slideup("slow", function() { // do something when slide up is finished $("#myelement").slidetoggle(1000, function() { // do something when slide up/down is finished To animate an element, you do so by telling jquery the CSS styles that the item should change to. jquery will set the new styles, but instead of setting them instantly (as CSS or raw JavaScript would do), it does so gradually, animating the effect at the chosen speed: $("#myelement").animate( { opacity:.3, width: "500px", height: "700px", 2000, function() { // optional callback after animation completes ); Animation with jquery is very powerful, and it does have its quirks (for example, to animate colors, you need a special plugin). It's worth taking the time to learn to use the animate command in-depth, but it is quite easy to use even for beginners. This is Just the Beginning There is so much more you can do with jquery beyond these basics I've introduced here. I highly recommend that all developers buy a good book on jquery, and also take the time to learn some important JavaScript concepts (like anonymous functions, closures, scope, etc.) in order to be able to use jquery in a more powerful way. And please note that I've introduced a lot of commands and syntaxes in a very superficial manner, without explaining many of the problems you could have with some of these, so if you do run into problems, you can check out the jquery documentation.