Chapter 10. jQuery UI: Extreme Form Makeover

image with no caption

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.

image with no caption
image with no caption
image with no caption

Below is a mockup of what our cryptozoologists want the new form to look like, along with a few extra notes.

image with no caption
image with no caption

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.

image with no caption

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.

Do this!

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:

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:

image with no caption

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:

  1. Build a datepicker for users to enter the date of the sighting.

  1. Build more engaging radio buttons for users to choose the creature type.

  1. Build number-entry sliders for users to enter distance from creature, creature weight, creature height, latitude, and longitude.

  1. Build a color mixer interface component for the user to enter creature color.

  1. Build a nicer-looking submit button for the sightings form.

It’s amazing how easy it is to put a jQuery UI widget into an HTML form. Let’s start with the calendar datepicker:

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.

image with no caption
image with no caption

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.

image with no caption

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.

image with no caption

Checklist item 1 is done. Let’s move on to checklist item 2.

1.

Build a datepicker for users to enter the date of the sighting.

2.

Build more engaging radio buttons for users to choose the creature type.

3.

Build number-entry sliders for users to enter distance from creature, creature weight, creature height, latitude, and longitude.

4.

Build a color mixer interface component for the user to enter creature color.

5.

Build a nicer-looking submit button for the sightings form.

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.

image with no caption

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.

image with no caption

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:

image with no caption
image with no caption

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.

image with no caption
image with no caption

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:

1.

Build a datepicker for users to enter the date of the sighting.

2.

Build more engaging radio buttons for users to choose the creature type.

3.

Build number-entry sliders for users to enter distance from creature, creature weight, creature height, latitude, and longitude.

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.

4.

Build a color mixer interface component for the user to enter creature color.

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.

image with no caption

So you need to build three different sliders: one for red, one for green, and one for blue. Then you’ll combine each of the slider’s values to become one color. Let’s look at what we need each slider widget to do.

image with no caption

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.

image with no caption
image with no caption

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:

image with no caption

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:

1.

Build a datepicker for users to enter the date of the sighting.

2.

Build more engaging radio buttons for users to choose the creature type.

3.

Build number-entry sliders for users to enter distance from creature, creature weight, creature height, latitude, and longitude.

4.

Build a color mixer interface component for the user to enter creature color.

5.

Build a nicer-looking submit button for the sightings form.

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!

image with no caption

OK, good point.

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.

image with no caption
image with no caption
image with no caption

Your jQuery Toolbox

You’ve got Chapter 10 under your belt and now you’ve added jQuery UI to your toolbox.