Skip to content
Tutorials Center
My learning

Topics

Lesson 39 of 56 ยท Plugins and Utilities

jQuery UI Dialog, Tabs, Accordion, Menu, Progressbar, Tooltip and Theming

Build page structure with jQuery UI: dialog, tabs, accordion, menu, progressbar and tooltip, plus widget factory methods, events and theming with ThemeRoller.

The second group of jQuery UI widgets organises content and gives feedback: dialog for modal windows, tabs and accordion for switching between panels, menu for lists of commands, progressbar for long tasks and tooltip for hover hints. Where the form widgets enhance one control, these enhance a block of markup. They all start from well-formed HTML, so the same page still works as a plain list of headings and paragraphs if the script does not load.

You will also see how every widget gets its methods from the same place, the widget factory ($.widget), and how to change the look with themes.

Layout and feedback widgets

Dialog A movable, modal window.
Tabs One panel visible at a time.
Accordion Collapsible stacked sections.
Menu Keyboard-friendly command list.
1 / 2

Dialog

.dialog() turns a <div> into a floating window with a title bar, a close button and optional buttons. The title attribute of the div becomes the dialog title.

Option Meaning
autoOpen false creates it closed so you open it later
modal blocks the page behind an overlay
width, height, minWidth, maxHeight sizing
buttons an object { Label: fn } or an array of button definitions
draggable, resizable default true for both
closeOnEscape default true
position where to place it, with { my, at, of } like .position()
show, hide effect for opening and closing, such as "fade"

Methods: "open", "close", "isOpen", "moveToTop", "destroy". Events: open, close, beforeClose (return false to stop closing), focus, dragStop, resizeStop. The dialog moves your div into a wrapper at the end of <body>, so event delegation from a parent of the original location will stop seeing its content; bind inside the dialog, or delegate from document.

For a simple modal on a new project, the native <dialog> element does most of this with no script. Keep jQuery UI's dialog for apps that already use it or need its drag and resize.

Tabs

.tabs() expects a list of links whose href values point to panel ids:

HTML
<div id="tabs">
  <ul>
    <li><a href="#overview">Overview</a></li>
    <li><a href="#specs">Specs</a></li>
    <li><a href="/fragments/reviews.html">Reviews</a></li>
  </ul>
  <div id="overview">...</div>
  <div id="specs">...</div>
</div>

A link whose href is a real URL becomes an AJAX tab: the plugin loads the response into a panel it creates. Options: active (index to open, or false for none), collapsible (a click on the active tab closes it), event ("mouseover" to switch on hover), heightStyle ("content", "auto", "fill"), disabled (array of indexes). Events: activate, beforeActivate, beforeLoad, load. Methods include "option", "refresh", "load" and "enable"/"disable" with an index.

Accordion

.accordion() takes alternating headers and panels (by default any heading followed by a div). Options: active, collapsible, header (a selector if you use something other than h3), heightStyle ("auto" makes all panels as tall as the tallest, "content" sizes each one), animate, and icons. By default only one panel is open at a time; for several open panels use plain <details> elements or call .accordion("option", "active", ...) on separate accordions.

.menu() makes a <ul> into a keyboard-navigable menu: arrow keys move, Enter selects, nested <ul> elements become submenus. It has no visual separation from the page, so it is often paired with a <button> that shows it. Events: select (an item was chosen; ui.item is the <li>), focus, blur. Mark items as headings or separators with <li>-</li>, and disable one with the class ui-state-disabled.

Progressbar

.progressbar({ value: 40 }) draws a bar. value: false gives an indeterminate bar with an animation for tasks with unknown duration. Read or change it with .progressbar("value") and .progressbar("value", 70); max defaults to 100. The change event fires on each update and complete when the value reaches the maximum. The bar already gets role="progressbar" and aria-valuenow, so assistive technology reads it.

Tooltip

.tooltip() replaces the browser's plain title popup with a styled one. Option content (a string or function) lets you show HTML, position controls placement, items is a selector for which descendants get tooltips (use it on a container so new elements are covered), and track: true makes it follow the pointer. Events: open and close.

Dialog, tabs, accordion and progressbar together

Open the dialog and notice the page behind it is dimmed and cannot be clicked: that is modal: true. The log shows which events fire when you change the active tab or panel from code; they fire the same way for clicks.

The widget factory

Every widget above (and every form widget) is built with $.widget, jQuery UI's factory for stateful plugins. It gives you, for free:

  • a consistent call style: .tabs(options), .tabs("method", args), .tabs("option", ...);
  • instance state you can read with .tabs("instance");
  • create, destroy, enable, disable, widget (returns the outer element) methods;
  • option merging, events named widgetnameevent, and clean-up on .remove().

This is also what you use to write your own widget (see Writing Your Own Plugin for the simpler plugin pattern first):

JavaScript
$.widget("ui.counter", {
  options: { start: 0 },
  _create: function () {
    this.value = this.options.start;
    this._on(this.element, { click: "increment" });
    this._render();
  },
  increment: function () { this.value++; this._render(); this._trigger("changed", null, { value: this.value }); },
  _render: function () { this.element.text(this.value); },
  _destroy: function () { this.element.text(""); }
});
$("#btn").counter({ start: 5, changed: function (e, d) { console.log(d.value); } });

Methods that start with an underscore stay private. Use this._on rather than raw .on so handlers are removed on destroy, and this._trigger so callbacks and events both work.

Theming and ThemeRoller

jQuery UI separates structure from skin. The structural CSS is always the same; the theme supplies colours, fonts and corner radii through classes such as ui-widget, ui-widget-header, ui-state-default, ui-state-hover, ui-state-active and ui-corner-all. Three practical routes:

  1. Use a ready theme. Replace themes/base/jquery-ui.min.css with another theme from the jQuery UI CDN, for example smoothness or dark-hive.
  2. Generate one with ThemeRoller. The online tool produces a downloadable stylesheet that you can host yourself.
  3. Override. Keep the base theme and add rules after it, as shown for form widgets. Because the ui-* classes are on every widget part, you can restyle a whole site with a few dozen lines.

For dark mode, jQuery UI has no automatic switch: load a dark theme (or override colours with CSS variables) when prefers-color-scheme: dark matches.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does the dialog option autoOpen: false do?

    autoOpen defaults to true. Setting false creates the widget closed so you open it from a button or event.
  2. 2. How do you make a tab load its panel content from a server?

    A link whose href is a URL creates an AJAX tab. The response is inserted into a panel that jQuery UI creates for you.
  3. 3. Which progressbar value shows an indeterminate (unknown length) animation?

    value: false switches the bar to indeterminate mode.
  4. 4. Why use this._on(...) inside a widget instead of this.element.on(...)?

    _on tracks the handlers it adds and unbinds them in destroy, which avoids leaks.
  5. 5. Which is NOT a standard way to change the look of jQuery UI widgets?

    There is no global theme option; skin comes from CSS.

Summary

  • Dialog, tabs, accordion, menu, progressbar and tooltip enhance blocks of markup and share the widget factory API.
  • Use autoOpen: false and modal for dialogs, href URLs for AJAX tabs, value: false for indeterminate progress.
  • Widgets raise events such as activate, close, complete; use beforeClose or beforeActivate to veto an action.
  • $.widget creates your own widget with private methods, _on and _trigger.
  • Theme by swapping the stylesheet, generating one with ThemeRoller, or overriding ui-* classes; consider native <dialog> and <details> for new work.

Next: look at an older sibling library in jQuery Mobile: Overview and Why It Faded.