The Web lives and dies by users and their data. Collecting data from users is a big business and can be a time-consuming challenge for a web developer. You’ve seen how jQuery can help make Ajax, PHP, and MySQL web apps work more effectively. Now let’s look at how jQuery can help us build the user interface for the forms that collect data from users. Along the way, you’ll get a healthy dose of jQuery UI, the official user interface library for jQuery.
Dr. Pattersby and Dr. Gimli are dedicated to collecting as much cryptid sighting data as possible from users around the world. Their website, cryptozoologists.org, is revered by professional and amateur cryptozoologists worldwide. The good doctors have another gig for you: update their very outdated Cryptid Sightings form.
Below is a mockup of what our cryptozoologists want the new form to look like, along with a few extra notes.
The crytozoologists have asked for a tall order here. They want you to build a user interface like you would find in a desktop application. What do you think jQuery offers that can accomplish this?
Frank: I saw it. The current form is an HTML form, but HTML and CSS aren’t going to cut it for the new form the cryptozoologists want.
Jim: Tell me about it. Have you ever tried to style form elements with CSS? I’d rather have a root canal.
Frank: Yeah, and jQuery...well, I haven’t seen anything in jQuery that will help us build interface components like that.
Joe: We’ve got to figure this out, guys. Folks are used to fancy components like this, so we’re going to have to find a way to build them.
Frank: We’ll probably need a combination of JavaScript, jQuery, and CSS to pull this one off.
Jim: That’s a lot of logic to write. Just the calendar pop up they want will be lines and lines of code and a bunch of complex CSS.
Joe: Hmmm. There may be a jQuery plug-in for this kind of stuff, actually.
Jim: A plug-in, right! We used one a couple chapters back to create tabs for the Bit to Byte race results page. So there’s more to plug-ins, huh?
Joe: Yeah, if jQuery doesn’t offer something a developer needs, that developer can build a plug-in and release it to the jQuery community for it to use. This saves other developers tons of hours.
Jim: So some developer or dev team out there may have already dealt with this?
Frank: That would really save us some headaches.
Joe: Let’s go poke around out at jQuery.com and see what we can find.
Fortunately for developers everywhere, jQuery has an official library of user interface plug-ins for just this kind of project. That library is called jQuery UI, and it offers three main types of plug-ins for the jQuery core: effects, interactions, and widgets.
Effects plug-ins | Interaction plug-ins | Widget plug-ins |
jQuery UI extends jQuery by adding more effects. Make your elements bounce, explode, pulsate, or shake. jQuery UIs also includes easing functions, complex mathematical operations that make animations look more realistic. | Interactions add more complex behavior to web apps. You can enable users to interact with elements by making those elements draggable, droppable, or sortable, just to name a few of the options. | A web widget is a self-contained component that adds functionality to your web app. Widgets save you tons of coding time and complexity while creating usable and responsive user interface elements. |
jQuery offers a plug-in architecture that allows web developers to extend (or add onto) the core jQuery library.
Before we can do anything with jQuery UI, we need to configure the components we want, choose a theme, and download a copy of it. Follow the steps below:
Point your browser to the jQuery UI download page:
http://jqueryui.com/download
Choose the components you want to download.
Choose a theme for your download
One of the best things about jQuery UI are its themes. The dev team for jQuery UI included all of the CSS for making a nice-looking interface. You can even create your own theme with jQuery UI’s “theme roller.” For a gallery of all the jQuery UI themes, visit the following URL:
Press the Download button.
You just need to unzip the folder and include the library in a project folder.
Turn the page, and we’ll have a look inside jQuery UI.
After downloading and unzipping jQuery, you’ll find that it’s structured like this:
We’ve included the jQuery UI folder in the code folder you downloaded at the beginning of the book. You’ll find it in the end folder inside the ch10 folder.
jQuery UI does a lot for you, but we’ve still got quite a few items to tackle in order to build the new form. Here’s a checklist of what we need to do:
It’s amazing how easy it is to put a jQuery UI widget into an HTML form. Let’s start with the calendar datepicker:
Create a link to the jQuery UI CSS file:
<link type="text/css" href="jquery-ui-1.8.16.custom/css/sunny/ jquery-ui-1.8.16.custom.css" rel="stylesheet" />
Create a <script>
tag that points to jQuery UI.
<script src="jquery-ui-1.8.16.custom/js/jquery-ui-1.8.16. custom.min.js"></script>
Take a plain old HTML input
field.
Add an ID of datepicker
to the <input>
tag.
<input type="text" name="sighting_date" id="datepicker">
Create a JavaScript file and put the following code between the curly braces of the $(document).ready(function(){}
.
$('#datepicker').datepicker();
Open the file in your favorite browser and click into the input
field.
It may seem a bit like magic, but jQuery UI is really just a lot of well-designed and well-written jQuery code—code that you didn’t have to write. Let’s take a closer look at how it works.
Don’t worry, you’ve got options.
Let’s check them out.
If you dig into the datepicker widget, you’ll find that it’s got a lot of rich features and options you can configure.
Because jQuery UI is built on jQuery, you don’t have to write much code to customize the datepicker widget to fit your needs. At the time of this writing, the datepicker has 46 different options you can set.
Checklist item 1 is done. Let’s move on to checklist item 2.
What does “more engaging” really mean? It’s mostly a question of style: make a better-looking button, and people are going to want to click it. One super-useful widget in the jQuery UI library is the button widget. The button widget offers a button
method to help you make more appealing form elements like submit buttons, radio buttons, and checkboxes.
For building a grouped set of buttons, jQuery UI offers the buttonset
method, which turns individual button elements into a group by referencing the container element for that group.
The plug-in for the jQuery UI slider gives you the power to build a slider interface that users can control with their mouse or keyboard. Sliders also help you control the numbers that users enter. As you’ve already seen, building widgets is a snap once you have the jQuery UI library. Building a slider widget is just as easy.
Sliders offer a bunch of customization options too. Let’s say we need the user to enter a set of numbers. The lowest value we want users to enter is 0; the maximum value is 100. And we want the users to enter values in increments of 5. Here’s how we can do that with the slider widget’s options:
We have to connect the slider with one of the slider widget’s event handlers.
We’ve seen some widget options, but we haven’t yet explored another powerful feature of jQuery UI. Many jQuery components offer event handlers, and the slider is one of them. At the time of this writing, jQuery UI’s slider widget offers five event handlers: create
, start
, slide
, change
, and stop
. To connect the slider to a form input, let’s try out the slide
event handler.
jQuery UI has you covered there, too.
The slider widget can deal with negative numbers and decimal numbers. You can enter negative numbers as values, as minimums, and as maximums. Give that a try below to see it in action.
So far, you’ve checked off quite a few items from the list:
What’s up next? Now you need to build the cryptozoologists an interface that will allow users to enter the color of the creature they saw by using sliders that represent red, green, and blue.
The values of red, green, and blue each have a minimum of 0 and a maximum of 255. When each color is at its minimum—in other words, when red’s value is 0, green’s is 0, and blue’s is 0—you get black. When each color is at its maximum—when red’s value is 255, green’s is 255, and blue’s is 255—you get white.
To finish our color mixer, we need a JavaScript function that will set and refresh the swatch. Here’s a skeleton version of that function, along with some of the key questions to consider before fleshing it out to get it to do what you want.
Good catch! We could write a decimal-to-hexadecimal conversion function, or we could use the decimal values straight from the sliders.
Remember that the CSS background-color
property allows us to specify colors like this:
But that’s just a hint for one of the questions. To write the whole function, you’ll have to do a little brain workout.
There’s just one item left on your checklist:
You’ve used buttons already in this chapter and you’ve been using buttons since Chapter 1, so this one should be pretty easy for you!
We’ve used jQuery’s click
method and jQuery UI’s button
method, but we haven’t done much with selecting form elements like input submit buttons. Here’s a quick guide on how to select those.
You’ve got Chapter 10 under your belt and now you’ve added jQuery UI to your toolbox.
An official jQuery library that offers three main types of plug-ins for the jQuery core: effects, interactions, and widgets.
A self-contained component that adds functionality to your web app.
Saves you tons of coding time and complexity while creating usable and responsive user interface elements.
Provides a button method to help you make more appealing form elements like submit buttons, radio buttons, and checkboxes.