You can create new widgets from scratch, using just the $.Widget object as a base to inherit from, or you can explicitly inherit from existing jQuery UI or third-party widgets. Defining a widget with the same name as you inherit from even allows you to extend widgets in place.

jQuery UI contains many widgets that maintain state and therefore have a slightly different usage pattern than typical jQuery plugins. All of jQuery UI's widgets use the same patterns, which is defined by the widget factory. So if you learn how to use one widget, then you'll know how to use all of them.

Note: This documentation shows examples using the progressbar widget but the syntax is the same for every widget.

Initialization

In order to track the state of the widget, we must introduce a full life cycle for the widget. The life cycle starts when the widget is initialized. To initialize a widget, we simply call the plugin on one or more elements.

1

$( "#elem" ).progressbar();

This will initialize each element in the jQuery object, in this case the element with an id of "elem".

Options

Because progressbar() was called with no parameters, the widget was initialized with its default options. We can pass a set of options during initialization to override the defaults:

1

$( "#elem" ).progressbar({ value: 20 });

We can pass as many or as few options as we want during initialization. Any options that we don't pass will just use their default values.

You can pass multiple options arguments. Those arguments will be merged into one object (similar to $.extend( true, target, object1, objectN )). This is useful for sharing options between instances, while overriding some properties for each one:

1

2

3

var options = { modal: true, show: "slow" };

$( "#dialog1" ).dialog( options );

$( "#dialog2" ).dialog( options, { autoOpen: false });

All options passed on init are deep-copied to ensure the objects can be modified later without affecting the widget. Arrays are the only exception, they are referenced as-is. This exception is in place to support data-binding, where the data source has to be kept as a reference.

The default values are stored on the widget's prototype, therefore we have the ability to override the values that jQuery UI sets. For example, after setting the following, all future progressbar instances will default to a value of 80:

1

$.ui.progressbar.prototype.options.value = 80;

The options are part of the widget's state, so we can set options after initialization as well. We'll see this later with the option method.

Methods

Now that the widget is initialized, we can query its state or perform actions on the widget. All actions after initialization take the form of a method call. To call a method on a widget, we pass the name of the method to the jQuery plugin. For example, to call the value() method on our progressbar widget, we would use:

1

$( "#elem" ).progressbar( "value" );

If the method accepts parameters, we can pass them after the method name. For example, to pass the parameter 40 to the value() method, we can use:

1

$( "#elem" ).progressbar( "value", 40 );

Just like other methods in jQuery, most widget methods return the jQuery object for chaining.

1

2

3

$( "#elem" )

.progressbar( "value", 90 )

.addClass( "almost-done" );

Each widget will have its own set of methods based on the functionality that the widget provides. However, there are a few methods that exist on all widgets, which are documented below.

Events

All widgets have events associated with their various behaviors to notify you when the state is changing. For most widgets, when the events are triggered, the names are prefixed with the widget name and lowercased. For example, we can bind to progressbar's change event which is triggered whenever the value changes.

1

2

3

$( "#elem" ).bind( "progressbarchange", function() {

alert( "The value has changed!" );

});

Each event has a corresponding callback, which is exposed as an option. We can hook into progressbar's change callback instead of binding to the progressbarchange event, if we want to.

1

2

3

4

5

$( "#elem" ).progressbar({

change: function() {

alert( "The value has changed!" );

}

});

All widgets have a create event which is triggered upon instantiation.

Instance

The widget's instance can be retrieved from a given element using the instance() method.

1

$( "#elem" ).progressbar( "instance" );

If the instance() method is called on an element that is not associated with the widget, undefined is returned.

1

$( "#not-a-progressbar" ).progressbar( "instance" ); // undefined

The instance is stored using jQuery.data() with the widget's full name as the key. Therefore, the :data selector can also determine whether an element has a given widget bound to it.

1

2

$( "#elem" ).is( ":data( 'ui-progressbar' )" ); // true

$( "#elem" ).is( ":data( 'ui-draggable' )" ); // false

Unlike instance(), :data can be used even if the widget being tested for has not loaded.

1

2

$( "#elem" ).nonExistentWidget( "instance" ); // TypeError

$( "#elem" ).is( ":data( 'ui-nonExistentWidget' )" ); // false

You can also use :data to get a list of all elements that are instances of a given widget.

1

$( ":data( 'ui-progressbar' )" );

Properties

All widgets have the following set of properties:

defaultElement: An element to use when a widget instance is constructed without providing an element. For example, since the progressbar's defaultElement is "<div>", $.ui.progressbar({ value: 50 }) instantiates a progressbar widget instance on a newly created <div>.

document: A jQuery object containing the document that the widget's element is within. Useful if you need to interact with widgets within iframes.

element: A jQuery object containing the element used to instantiate the widget. If you select multiple elements and call .myWidget(), a separate widget instance will be created for each element. Therefore, this property will always contain one element.

namespace: The location on the global jQuery object that the widget's prototype is stored on. For example a namespace of "ui" indicates that the widget's prototype is stored on $.ui.

options: An object containing the options currently being used by the widget. On instantiation, any options provided by the user will automatically be merged with any default values defined in $.myNamespace.myWidget.prototype.options. User specified options override the defaults.

uuid: A unique integer identifier for the widget.

version: The string version of the widget. For jQuery UI widgets this will be set to the version of jQuery UI the widget is using. Widget developers have to set this property in their prototype explicitly.

widgetEventPrefix: The prefix prepended to the name of events fired from this widget. For example the widgetEventPrefix of the draggable widget is "drag", therefore when a draggable is created, the name of the event fired is "dragcreate". By default the widgetEventPrefix of a widget is its name. Note: This property is deprecated and will be removed in a later release. Event names will be changed to widgetName:eventName (e.g. "draggable:create").

widgetFullName: The full name of the widget including the namespace. For $.widget( "myNamespace.myWidget", {} ), widgetFullName will be "myNamespace-myWidget".

widgetName: The name of the widget. For $.widget( "myNamespace.myWidget", {} ), widgetName will be "myWidget".

window: A jQuery object containing the window that the widget's element is within. Useful if you need to interact with widgets within iframes.

hide

Boolean:
When set to false, no animation will be used and the element will be hidden immediately.
When set to true, the element will fade out with the default duration and the default easing.

Number:
The element will fade out with the specified duration and the default easing.

String:
The element will be hidden using the specified effect.
The value can either be the name of a built-in jQuery animation method, such as "slideUp", or the name of a jQuery UI effect, such as "fold".
In either case the effect will be used with the default duration and the default easing.

Object: If the value is an object, then effect, delay, duration, and easing properties may be provided. If the effect property contains the name of a jQuery method, then that method will be used; otherwise it is assumed to be the name of a jQuery UI effect. When using a jQuery UI effect that supports additional settings, you may include those settings in the object and they will be passed to the effect. If duration or easing is omitted, then the default values will be used. If effect is omitted, then "fadeOut" will be used. If delay is omitted, then no delay is used.

show

Boolean:
When set to false, no animation will be used and the element will be shown immediately.
When set to true, the element will fade in with the default duration and the default easing.

Number:
The element will fade in with the specified duration and the default easing.

String:
The element will be shown using the specified effect.
The value can either be the name of a built-in jQuery animation method, such as "slideDown", or the name of a jQuery UI effect, such as "fold".
In either case the effect will be used with the default duration and the default easing.

Object: If the value is an object, then effect, delay, duration, and easing properties may be provided. If the effect property contains the name of a jQuery method, then that method will be used; otherwise it is assumed to be the name of a jQuery UI effect. When using a jQuery UI effect that supports additional settings, you may include those settings in the object and they will be passed to the effect. If duration or easing is omitted, then the default values will be used. If effect is omitted, then "fadeIn" will be used. If delay is omitted, then no delay is used.

This method allows the widget to define a custom method for defining options during instantiation. The user-provided options override the options returned by this method, which override the default options.

Widgets have the concept of initialization that is distinct from creation. Any time the plugin is called with no arguments or with only an option hash, the widget is initialized; this includes when the widget is created.

Note: Initialization should only be handled if there is a logical action to perform on successive calls to the widget with no arguments.

Automatically handles disabled widgets: If the widget is disabled or the event occurs on an element with the ui-state-disabled class, the event handler is not invoked. Can be overridden with the suppressDisabledCheck parameter.

Event handlers are automatically namespaced and cleaned up on destroy.

Sets the value of the widget option associated with the specified optionName.

Note: For options that have objects as their value, you can set the value of just one property by using dot notation for optionName. For example, "foo.bar" would update only the bar property of the foo option.