Book Image

jQuery for Designers: Beginner's Guide

By : Natalie Maclees
Book Image

jQuery for Designers: Beginner's Guide

By: Natalie Maclees

Overview of this book

jQuery is awesome for designers ñ it builds easily on the CSS and HTML you already know and allows you to create impressive effects with just a few lines of code. However, without a background in programming, JavaScript ñ on which jQuery is built ñ can feel intimidating and impossible to grasp. This book will show you how simple it can be to learn the basics and then extend your capabilities by taking advantage of jQuery plugins.jQuery for Designers offers approachable lessons for designers with little or no background in JavaScript. The book begins by introducing the jQuery library and a small and simple introduction to JavaScript. Then you'll step through a few simple tasks to get your feet wet before diving into using plugins to quickly and simply add complex effects with just a few lines of code.You'll be surprised at how far you can get with JavaScript when you start with the power of the jQuery library and this book will show you how. We'll cover common interface widgets and effects such as tabbed interfaces, custom tooltips, and custom scrollbars. You'll learn how to create an animated navigation menu and how to add simple AJAX effects to enhance your site visitors' experience. Then we'll wrap up with interactive data grids which make sorting and searching data easy.
Table of Contents (19 chapters)
jQuery for Designers Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – getting ready for jQuery


  1. Set up your files and folders just like we did in the previous exercise. Inside the <body> of the HTML document, add a heading and a paragraph:

    <body>
         <h1>My First jQuery</h1>
         <p>Thanks to jQuery doing fancy JavaScript stuff is easy.</p>
    </body>
  2. Feel free to create some CSS in the styles.css in the styles folder—style this however you would like.

  3. Next, open up that empty scripts.js file we created earlier and add this bit of script to the file:

    $(document).ready();

What just happened?

Let's take this statement one at a time—first a dollar sign? Really? What's that doing in JavaScript?

The $ here is just a variable—that's all. It's a container for the jQuery function. Remember how I said we might use a variable to save ourselves a few keystrokes? The clever writers of jQuery have provided the $ variable to save us from having to write out jQuery every time we want to use it. This code does the same thing:

jQuery(document).ready();

Except it takes longer to type. jQuery uses the $ as its short name because it's unlikely that you'd call a variable $ on your own since it's an uncommon character. Using an uncommon character reduces the chance that there would be some sort of conflict between some other JavaScript being used on a page and the jQuery library.

So, in this case, we're passing document to the jQuery or $ method, because we want to select our HTML document as the target of our code. When we call the jQuery function, we get a jQuery object. In JavaScript speak, we would say that the jQuery function returns a jQuery object. The jQuery object is what gives the jQuery library its power. The entire jQuery library exists to give the jQuery object lots of properties and methods that make our lives easier. We don't have to deal with lots of different sorts of objects—we just have to deal with the jQuery object.

The jQuery object has a method called ready(). In this case, the ready method will be called when the document is loaded into the browser, and is ready for us to work with. So $(document).ready() just means, "when the document is ready".

Adding a paragraph

So now we're all set to do something when the document is ready, but what is it that we'll do? Let's add a new paragraph to our page.