Skip to content
Tutorials Center
My learning

Topics

Lesson 38 of 56 ยท Plugins and Utilities

jQuery UI Form Widgets: Autocomplete, Datepicker, Slider, Spinner and More

Enhance forms with jQuery UI: autocomplete, datepicker, slider, spinner, selectmenu, button, checkboxradio and controlgroup, with options, events and theming.

Forms are where jQuery UI gets used most. A plain <input> becomes a search box with suggestions, a calendar popup, a range slider or a number spinner, while the real form control stays in the page and still submits normally. This is the key idea behind every widget in this lesson: the widget enhances a native element, it does not replace it. You read and write the value through the same input you always did, and a form post works without any extra code.

You should already know how to load jQuery UI and its stylesheet from Using Plugins and jQuery UI, and how forms work from Forms and Values. The examples load jQuery 3.7.1 and jQuery UI 1.14.1 from a CDN.

Form widgets at a glance

Autocomplete Suggestions while typing.
Datepicker A calendar popup.
Slider Pick a value or a range.
Spinner Up and down arrows for numbers.
1 / 2

Autocomplete

.autocomplete({ source }) shows a menu of suggestions as the user types. The source can be:

  • an array of strings or { label, value } objects (jQuery UI filters it for you);
  • a URL that returns JSON (it gets a term query parameter);
  • a function (request, response), the most flexible form: you read request.term and call response(items) when you have results, which makes asynchronous sources easy.

Useful options: minLength (characters before searching, default 1), delay (ms, default 300), autoFocus, and appendTo. Events: search, open, focus, select, change, close. In select, ui.item is the chosen entry; return false to stop jQuery UI from writing ui.item.value into the input.

Autocomplete with labels and values

Datepicker

.datepicker(options) attaches a calendar to a text input. It is the oldest widget in the library and has the most options:

Option Meaning
dateFormat "yy-mm-dd", "dd/mm/yy", "DD, d MM yy"; note yy is a four-digit year
minDate, maxDate a Date, "+1w", 0 (today) or "-1y"
changeMonth, changeYear dropdowns for month and year
numberOfMonths show two or more months side by side
showWeek, firstDay week numbers, first weekday (0 is Sunday)
beforeShowDay a function that returns [selectable, cssClass, tooltip] for each day, which is how you block weekends or holidays
inline use .datepicker() on a <div> for an always-visible calendar

Read the chosen date as a Date with .datepicker("getDate"), or format one with $.datepicker.formatDate("yy-mm-dd", date). Localisation comes from extra datepicker-xx.js files that set regional defaults.

A native <input type="date"> now covers the plain case with no library and a better mobile UI. Pick the datepicker when you need rules such as blocking particular days or showing several months.

Slider

.slider() turns a <div> into a draggable handle. It has no input of its own, so you connect it to one in the slide or change event. Options: min, max, step, value, orientation ("horizontal" or "vertical"), animate, and range. With range: true the slider gets two handles and you use values: [20, 80]; with range: "min" or "max" the part below or above the handle is filled.

The slide event fires on every move and can be cancelled by returning false; change fires when the value changes after the user lets go or when code changes it.

Spinner

.spinner() adds up and down buttons to an <input> and enables arrow-key and mouse-wheel stepping. Options: min, max, step, page (how far PageUp/PageDown jump), incremental (speed up while held) and numberFormat (needs the Globalize library, so most pages skip it). Methods "value", "stepUp", "stepDown" and "pageUp" let code control it. Events are spin (while stepping), change and stop.

Slider, spinner and a shared value

When a widget and a form field must stay in sync, update each one from the other's event, as above. Setting a value from code does fire change on a slider but not slide, so you avoid endless loops by listening to slide (user only) and spin.

Selectmenu, button, checkboxradio and controlgroup

These four widgets exist to give native controls a consistent, themed look.

  • .selectmenu() replaces the pop-up of a <select> with a styled menu that supports icons and custom item rendering. Events: open, close, select, change. It reads the real <select>, so disabled and optgroup still work. On phones, a native select is usually the better user experience.
  • .button() (called .button() on <button>, <input type="submit"> and <a>) adds theme classes, optional icon and iconPosition, and showLabel: false for icon-only buttons.
  • .checkboxradio() turns a checkbox or radio plus its <label> into a toggle button. It needs the label to be associated with the input (for/id). Options include icon, classes and disabled.
  • .controlgroup() joins adjacent controls (buttons, selectmenus, spinners, checkboxradios) into one connected bar with matching corners. It takes direction: "vertical" for a stack.
Selectmenu, checkboxradio and controlgroup

When you change checked, selected or disabled from code, tell the widget with .checkboxradio("refresh") or .selectmenu("refresh"); otherwise it keeps showing the old state.

Theming

jQuery UI looks the way it does because of a CSS file. The base theme used above is deliberately plain. The ThemeRoller tool on the jQuery UI site (and downloadable themes such as smoothness and ui-lightness) generates a stylesheet that sets fonts, colours and corner radii. For custom branding, write your own rules that override the framework classes:

CSS
.ui-widget { font-family: "Inter", system-ui, sans-serif; }
.ui-button.ui-state-hover { background: #0ea5e9; color: #fff; }
.ui-slider .ui-slider-handle { border-radius: 50%; }

Widgets also accept a classes option that adds your own class names to the parts they create, for example .slider({ classes: { "ui-slider-handle": "my-handle" } }), which is cleaner than a deep selector.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. When you give autocomplete a function as source, how do you return the suggestions?

    The function receives (request, response). Call response(items), which lets you do it asynchronously after an AJAX request.
  2. 2. Which datepicker option lets you disable weekends?

    beforeShowDay runs for each date and returns [selectable, cssClass, tooltip], so you can return [false] for weekends and holidays.
  3. 3. What is the correct way to create a slider with two handles?

    range: true creates a range slider and values holds both handle positions.
  4. 4. You set a checkbox with .prop("checked", true) but the checkboxradio button looks unchecked. What do you call?

    The widget does not watch the underlying input. refresh makes it re-read the state.
  5. 5. What is true of jQuery UI form widgets in general?

    The real input or select stays, so values are read and submitted the usual way.

Summary

  • jQuery UI form widgets enhance native controls; the underlying input still holds the value and submits.
  • autocomplete takes an array, a URL or a (request, response) function; datepicker handles formats and rules like beforeShowDay.
  • slider (with range) and spinner need to be kept in sync through their events.
  • selectmenu, checkboxradio, button and controlgroup give native controls a themed, consistent look; call refresh after changing state from code.
  • Theme with ThemeRoller or override .ui-* classes and the classes option, and prefer native controls when they are enough.

Next: dialogs, tabs and layout widgets in jQuery UI Layout Widgets.