Jqueryui Tutorial
Jqueryui Tutorial
Audience
This tutorial has been prepared for anyone who has a basic knowledge of HTML and CSS
and has an urge to develop websites. After completing this tutorial, you will find yourself
at a moderate level of expertise in developing web projects using Twitter JqueryUI.
Prerequisites
Before you start proceeding with this tutorial, I'm making an assumption that you are
already aware about basics of HTML and CSS. If you are not well aware of these concepts,
then I will suggest to go through our short tutorial on HTML Tutorial and CSS Tutorial.
JQueryUI
Table of Contents
About the Tutorial .......................................................................................................................................... i
Audience ......................................................................................................................................................... i
Prerequisites ................................................................................................................................................... i
Disclaimer & Copyright ................................................................................................................................... i
Table of Contents........................................................................................................................................... ii
1.
2.
4.
5.
JQueryUI
JQueryUI Selectable............................................................................................................................ 66
$ (selector, context).selectable (options) Method ...................................................................................... 66
Default Functionality ................................................................................................................................... 68
Use of Delay and Distance ........................................................................................................................... 69
Use of Filter ................................................................................................................................................. 71
$ (selector, context).selectable ("action", params) Method ....................................................................... 73
Example ....................................................................................................................................................... 75
Event Management on Selectable Elements ............................................................................................... 77
Example ....................................................................................................................................................... 79
7.
9.
JQueryUI
JQueryUI
JQueryUI
vi
1. JQUERY INTRODUCTION
JQueryUI
JqueryUI is a powerful Javascript library built on top of jQuery JavaScript library. UI stands
for User interface, It is a set of plug-ins for jQuery that adds new functionalities to the
jQuery core library.
The set of plug-ins in JqueryUI includes interface interactions, effects, animations, widgets,
and themes built on top of jQuery JavaScript Library.
It was released in September 2007, announced in a blog post by John Resig on [Link].
The latest release, 1.10.4, requires jQuery 1.6 or later version. jQuery UI is a free, open
source software, licensed under the MIT License.
Features
JqueryUI is categorized into four groups namely, Interactions, Widgets, Effects, and
Utilities.
These will be discussed in detail in the subsequent chapters. The structure of the library
is as shown in the image below:
Interactions : These are the interactive plugins like drag, drop, resize and more
which give the user the ability to interact with DOM elements.
Widgets : Using widgets which are jQuery plugins, you can create user interface
elements like accordian, datepicker, etc.
JQueryUI
Effects : These are built on the internal jQuery effects. They contain a full suite of
custom animations and transitions for DOM elements.
Utilities : These are a set of modular tools the JqueryUI library uses internally.
Benefits of JqueryUI
The below are some of the benefits of Jquery UI:
JQueryUI
This chapter will discuss about download and set up of JqueryUI library. We will also briefly
study the directory structure and its contents. JqueryUI library can be used in two ways
in your web page:
Stable : Click on this button to get the stable and latest version of JqueryUI library.
Legacy : Click on this button to get the previous major release of the JqueryUI
library.
JQueryUI
This is useful when you require only specific plugins or features of the JqueryUI library.
The directory structure of this version is shown in the following figure:
JQueryUI
Stable Download
Click on the Stable button, which leads directly to a ZIP file containing the sources,
examples, and documentation for latest version of JqueryUI library. Extract the ZIP file
contents to a jqueryui directory.
This version contains all files including all dependencies, a large collection of demos, and
even the librarys unit test suite. This version is helpful to getting started.
Legacy Download
Click on the Legacy button, which leads directly to a ZIP file of previous major release of
JqueryUI library. This version also contains all files including all dependencies, a large
collection of demos, and even the librarys unit test suite. This version is helpful to get you
started.
JQueryUI
Example
Now let us write a simple example using JqueryUI. Let us create an HTML file, copy the
following content to the <head> tag:
<link href="[Link]
rel="stylesheet">
<script src="[Link]
<script src="[Link]
Details of the above code are:
The first line, adds jQuery UI theme (in our case ui-lightness) via CSS. This CSS
will make our UI stylish.
Second line, adds the jQuery library, as jQuery UI is built on top of jQuery library.
Third line, adds the jQuery UI library. This enables jQuery UI in your page.
JQueryUI
$('#dialogMsg').dialog();
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="dialogMsg" title="First JqueryUI Example">
Hello this is my first JqueryUI example.
</div>
</form>
</body>
<html>
Open the above page in your browser. It will produce the following screen.
JQueryUI
3. JQUERYUI DRAGGABLE
JQueryUI
jQueryUI provides draggable() method to make any DOM element draggable. Once the
element is draggable, you can move that element by clicking on it with the mouse and
dragging it anywhere within the viewport.
Syntax
The draggable() method can be used in two forms:
Syntax
$(selector, context).draggable(options);
You can provide one or more options at a time using Javascript object. If there are more
than one options to be provided then you will separate them using a comma as follows:
$(selector, context).draggable({option1: value1, option2: value2..... });
The following table lists the different options that can be used with this method:
Option
Description
If this option is set to false, it will prevent the uidraggable class from being added in the list of selected DOM
elements. By default its value is true.
addClasses
Syntax
$(".selector").draggable(
{ addClasses: false }
);
appendTo
JQueryUI
$(".selector").draggable(
{ appendTo: "body"}
);
This option constrains dragging to either the horizontal (x)
or vertical (y) axis. Possible values: "x", "y".
Syntax
axis
$(".selector").draggable(
{ axis: "x" }
);
You can use this option to prevent dragging from starting
on specified elements. By default its value is
"input,textarea, button,select,option".
cancel
Syntax
$(".selector").draggable(
{ cancel: ".title" }
);
You can use this option to specify a list whose elements are
interchangeable. At the end of placement, the element is
part of the list. By default its value is "false".
connectToSortable
Syntax
$(".selector").draggable(
{ connectToSortable: "#my-sortable" }
);
Constrains dragging to within the bounds of the specified
element or region. By default its value is "false".
containment
Syntax
$(".selector").draggable(
{ containment: "parent" }
);
10
JQueryUI
"crosshair" (across)
"pointer" (hand)
"auto" (default)
11
JQueryUI
"wait" (hourglass)
Syntax
$(".selector").draggable(
{ cursor: "crosshair" }
);
Sets the offset of the dragging helper relative to the mouse
cursor. Coordinates can be given as a hash using a
combination of one or two keys: { top, left, right, bottom
}. By default its value is "false".
cursorAt
Syntax
$(".selector").draggable(
$( ".selector" ).draggable({ cursorAt: { left: 5 } });
);
Delay, in milliseconds, after which the first movement of
the mouse is taken into account. The displacement may
begin after that time. By default its value is "0".
Syntax
delay
$(".selector").draggable(
{ delay: 300 }
);
When set to true, disables the ability to move items. Items
cannot be moved until this function is enabled (using the
draggable ("enable") instruction). By default its value is
"false".
disabled
Syntax
$(".selector").draggable(
{ disabled: true }
);
12
JQueryUI
Syntax
$(".selector").draggable(
{ distance: 10 }
);
Snaps the dragging helper to a grid, every x and y pixels.
The array must be of the form [ x, y ]. By default its value
is "false".
grid
Syntax
$(".selector").draggable(
{ grid: [ 50, 20 ] }
);
If specified, restricts dragging from starting unless the
mousedown occurs on the specified element(s). By default
its value is "false".
handle
Syntax
$(".selector").draggable(
{ handle: "h2" }
);
Allows for a helper element to be used for dragging display.
By default its value is "original".
Syntax
helper
$(".selector").draggable(
{ helper: "clone" }
);
Prevent iframes from capturing the mousemove events
during a drag. By default its value is "false".
Syntax
iframeFix
$(".selector").draggable(
{ iframeFix: true }
);
13
JQueryUI
opacity
$(".selector").draggable(
{ opacity: 0.35 }
);
If set to true, all droppable positions are calculated on every
mousemove. By default its value is "false".
Syntax
refreshPositions
$(".selector").draggable(
{ refreshPositions: true }
);
Indicates whether the element is moved back to its original
position at the end of the move. By default its value is
"false".
revert
Syntax
$(".selector").draggable(
{ revert: true }
);
Duration of displacement (in milliseconds) after which the
element returns to its original position (see [Link]).
By default its value is "500".
revertDuration
scope
JQueryUI
{ scope: "tasks" }
);
When set to true (the default), the display will scroll if the
item is moved outside the viewable area of the window. By
default its value is "true".
scroll
Syntax
$(".selector").draggable(
{ scroll: false }
);
Indicates how many pixels the mouse must exit the window
to cause scrolling of the display. By default its value is "20".
Syntax
scrollSensitivity
$(".selector").draggable(
{ scrollSensitivity: 100 }
);
$(".selector").draggable(
{ scrollSpeed: 100 }
);
Adjusts the display of the item being moved on other
elements (which are flown). By default its value is "false".
Syntax
snap
$(".selector").draggable(
{ snap: true }
);
snapMode
15
JQueryUI
$(".selector").draggable(
{ snapMode: "inner" }
);
Maximum number of pixels in the difference in position
necessary to establish the adjustment. By default its value
is "20".
snapTolerance
Syntax
$(".selector").draggable(
{ snapTolerance: 30 }
);
Controls the z-index of the set of elements that match the
selector, always brings the currently dragged item to the
front. Very useful in things like window managers. By
default its value is "false".
stack
Syntax
$(".selector").draggable(
{ stack: ".products" }
);
Z-index for the helper while being dragged. By default its
value is "false".
Syntax
zIndex
$(".selector").draggable(
{ zIndex: 100 }
);
The following section will show you a few working examples of drag functionality.
Default Functionality
The following example demonstrates a simple example of draggable functionality passing
no parameters to the draggable() method .
<!DOCTYPE html>
<head>
<link href="[Link] rel="stylesheet">
16
JQueryUI
<script src="[Link]
<script src="[Link]
<style>
#draggable { width: 150px; height: 150px; padding: 0.5em; background:#eee;}
</style>
<script>
$(function() {
$( "#draggable" ).draggable();
});
</script>
</head>
<body>
<div id="draggable" class="ui-widget-content">
<p>Drag me !!!</p>
</div>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser that supports javascript. You should see the following output. Now, you can play
with the result:
Drag me !!!
JQueryUI
<script>
$("#div1 span").draggable (
{ disabled: true }
);
$("#div2 span").draggable (
{ distance: 50 }
);
$("#div3 span").draggable (
{ delay: 500 }
);
</script>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser that supports javascript, you should see the following output. Now, you can play
with the result:
You can't move me!
18
JQueryUI
Constrain Movement
The following example shows how to limit the movement of elements on the screen using
containment option in the drag function of JqueryUI.
<!DOCTYPE html>
<head>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
</head>
<body>
<div id="div4" style="border:solid 1px;background-color:gainsboro;">
<span>You can drag me only within this div.</span><br /><br />
</div>
<div id="div5" style="border:solid 1px;background-color:grey;">
<span>You can drag me only along x axis.</span><br /><br />
</div>
<script>
$("#div4 span").draggable ({
containment : "#div4"
});
$("#div5 span").draggable ({
axis : "x"
});
</script>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript. It should produce the following output. Now, you can
play with the output:
You can drag me only within this div.
19
JQueryUI
Here, <span> elements are prevented from going outside a <div> whose ID is div4. You
can also impose constraints on vertical or horizontal motion using options axis worth "x"
or "y", which is also demonstrated.
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output:
You can duplicate me....
As you can see when the first element is being dragged, only the cloned element moves,
while the original item stays put. If you release the mouse, the cloned element disappears
and the original item is still in its original position.
20
JQueryUI
<script>
/* First make the item draggable */
$("#divX span").draggable();
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output:
Click anywhere on me to see cursor type...
21
JQueryUI
Syntax
$(selector, context).draggable ("action", [params]);
The following table lists the actions for this method:
Action
destroy()
Description
Remove drag functionality completely. The elements are no
longer movable. This will return the element back to its preinit state.
Syntax
$(".selector").draggable("destroy");
Disable drag functionality. Elements cannot be moved until
the next call to the draggable("enable") method.
disable()
Syntax
$(".selector").draggable("disable");
Reactivates drag management. The elements can be moved
again.
enable()
Syntax
$(".selector").draggable("enable");
Gets
the
value
currently
associated
with
the
specifiedoptionName. Where optionName is name of the
option to get and is of type String.
option(optionName)
Syntax
var isDisabled = $( ".selector" ).draggable( "option",
"disabled" );
Gets an object containing key/value pairs representing the
current draggable options hash.
option()
Syntax
var options = $( ".selector" ).draggable( "option" );
22
JQueryUI
option(optionName, value)
option(options)
Syntax
$(".selector").draggable("option", { disabled: true } );
Returns a jQuery object containing the draggable element.
Syntax
widget()
Example
Now let us see an example using the actions from the above table. The following example
demonstrates the use of actions disable and enable.
<!DOCTYPE html>
<head>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
</head>
<body>
<div id="div7" style="border:solid 1px;background-color:gainsboro;">
<span>You can't move me. Dragging is disabled.</span><br><br>
</div>
<div id="div8" style="border:solid 1px;background-color:grey;">
<span>You can move me. Dragging is enabled.</span><br><br>
</div>
<script>
$("#div7 span").draggable ();
$("#div7 span").draggable ('disable');
$("#div8 span").draggable ();
$("#div8 span").draggable ('enable');
23
JQueryUI
</script>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you should see the following output:
You can't move me. Dragging is disabled.
You can move me. Dragging is enabled.
As you can see first element is disabled and the second element's dragging is enabled
which you can try to drag.
Description
Triggered when the draggable is created. Where event is of type Event,
and ui is of type Object.
Syntax
create(event, ui)
$(".selector").draggable(
create: function(event, ui) {}
);
Triggered while the mouse is moved during the dragging. Where event
is of type Event, and ui is of type Object like helper, position, offset.
Syntax
drag(event, ui)
$(".selector").draggable(
drag: function(event, ui) {}
);
Triggered when dragging starts. Where event is of type Event, and ui is
of type Object like helper, position, offset.
start(event, ui)
Syntax
$(".selector").draggable(
24
JQueryUI
stop(event, ui)
$(".selector").draggable(
stop: function( event, ui ) {}
);
Example
The following example demonstrates the use of event method during drag functionality.
This example demonstrates use of drag event.
<!DOCTYPE html>
<head>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
</head>
<body>
<div id="div9" style="border:solid 1px;background-color:gainsboro;">
<span>Drag me to check the event method firing</span><br /><br />
</div>
<script>
$("#div9 span").draggable ({
cursor: "move",
axis : "x",
drag: function( event, ui ){
alert("hi..");
}
});
</script>
</body>
</html>
25
JQueryUI
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you should see the following output:
Drag me to check the event method firing
Now try to drag the written content and you will see that start of a drag event gets fired
which results in showing a dialogue box and cursor will change to move icon and text will
move in X-axis only.
26
4. JQUERYUI DROPPABLE
JQueryUI
jQueryUI provides draggable() method to make any DOM element draggable. Once the
element is draggable, you can move that element by clicking on it with the mouse and
dragging it anywhere within the viewport.
Syntax
The draggable() method can be used in two forms:
Syntax
$(selector, context).draggable(options);
You can provide one or more options at a time using Javascript object. If there are more
than one options to be provided then you will separate them using a comma as follows:
$(selector, context).draggable({option1: value1, option2: value2..... });
The following table lists the different options that can be used with this method:
Option
accept
Description
This option is used when you need to control which draggable elements
are to be accepted for dropping. By default its value is * meaning that
every item is accepted by droppable.
This can be of type:
Selector: This type indicates which draggable elements are accepted.
Function: A callback function will be called for each draggable element on
page. This function should return true if the draggable element is accepted
by droppable.
Syntax
$( ".selector" ).droppable(
{ accept: ".special" }
27
JQueryUI
);
activeClass
addClasses
This option when set to false will prevent the ui-droppable class from being
added to the droppable elements. By default its value is true.
This may be desired as a performance optimization when calling
.droppable() init on hundreds of elements.
Syntax
$( ".selector" ).droppable(
{ addClasses: false }
);
disabled
This option when set to true disables the droppable. By default its value
is false.
Syntax
$( ".selector" ).droppable(
{ disabled: true }
);
28
JQueryUI
greedy
This option is used when you need to control which draggable elements
are to be accepted for dropping on nested droppables. By default its value
is false. If this option is set to true, any parent droppables will not receive
the element.
Syntax
$( ".selector" ).droppable(
{ greedy: true }
);
hoverClass
scope
$( ".selector" ).droppable(
{ scope: "tasks" }
);
tolerance
This option is a String that specifies which mode to use for testing whether
a draggable is hovering over a droppable. By default its value
is "intersect".
Possible values are:
29
JQueryUI
Syntax
$( ".selector" ).droppable(
{ tolerance: "fit" }
);
The following section will show you a few working examples of drag functionality.
Default Functionality
The following example demonstrates a simple example of draggable functionality passing
no parameters to the draggable() method .
<!DOCTYPE html>
<head>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<style>
#draggable { width: 150px; height: 150px; padding: 0.5em; background:#eee;}
</style>
<script>
$(function() {
$( "#draggable" ).draggable();
});
</script>
</head>
<body>
<div id="draggable" class="ui-widget-content">
<p>Drag me !!!</p>
</div>
30
JQueryUI
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output. Now, you can
play with the result:
Drag me !!!
<script>
31
JQueryUI
$("#div1 span").draggable (
{ disabled: true }
);
$("#div2 span").draggable (
{ distance: 50 }
);
$("#div3 span").draggable (
{ delay: 500 }
);
</script>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you should see the following output. Now, you can play
with the result:
You can't move me!
Constrain Movement
The following example shows how to limit the movement of elements on the screen using
containment option in the drag function of JqueryUI.
<!DOCTYPE html>
<head>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
</head>
<body>
<div id="div4" style="border:solid 1px;background-color:gainsboro;">
32
JQueryUI
Here, <span> elements are prevented from going outside a <div> whose ID is div4. You
can also impose constraints on vertical or horizontal motion using options axis worth "x"
or "y", which is also demonstrated.
JQueryUI
<body>
<div id="div6" style="border:solid 1px;background:#eee; height:50px;">
<span>You can duplicate me....</span>
</div>
<script>
$("#div6 span").draggable ({
helper : "clone"
});
</script>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you should see the following output:
You can duplicate me....
As you can see first element is being dragged-only the cloned element is moved, while the
original item remains in its original position. If you release the mouse, the cloned element
disappears and the original item is still in its original position.
<script>
34
JQueryUI
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output:
Click anywhere on me to see cursor type...
Syntax
$(selector, context).draggable ("action", [params]);
The following table lists the actions for this method:
Action
destroy()
Description
Remove drag functionality completely. The elements are no
longer movable. This will return the element back to its preinit state.
Syntax
$( ".selector" ).droppable("destroy");
disable()
JQueryUI
Syntax
$( ".selector" ).droppable("disable");
enable()
option(optionName)
Gets
the
value
currently
associated
with
the
specifiedoptionName. Where optionName is name of the
option to get and is of type String.
Syntax
var isDisabled = $( ".selector" ).droppable( "option",
"disabled" );
option()
option(optionName, value)
Syntax
$( ".selector" ).droppable( "option", "disabled", true );
option(options)
widget()
36
JQueryUI
Example
Now let us see an example using the actions from the above table. The following example
demonstrates the use of destroy() method.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Droppable - Default functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<style>
.draggable-4 {
width: 90px; height: 50px; padding: 0.5em; float: left;
margin: 0px 5px 10px 0;
border: 1px solid red;
background-color:#B9CD6D;
}
.droppable-7 {
width: 100px; height: 90px;padding: 0.5em; float: left;
margin: 10px;
border: 1px solid black;
background-color:#A39480;
}
.[Link] {
background-color: red;
}
</style>
<script>
$(function() {
$('.draggable-4').draggable({ revert: true });
$('.droppable-7').droppable({
hoverClass: 'active',
drop: function(e, ui) {
37
JQueryUI
$(this).html([Link]().html());
$(this).droppable('destroy');
$( this )
.addClass( "ui-state-highlight" )
.find( "p" )
.html( "i'm destroyed!" );
}
});
});
</script>
</head>
<body>
<div class="draggable-4"><p>drag 1</p></div>
<div class="draggable-4"><p>drag 2</p></div>
<div class="draggable-4"><p>drag 3</p></div>
Drag 2
Drag 3
38
JQueryUI
Drop Here
Drop Here
Drop Here
activate(event, ui)
Description
This event is triggered when the accepted draggable element
starts dragging. This can be useful if you want to make the
droppable "light up" when it can be dropped on.
Where event is of type Event, and ui is of type Object. Possible
values of ui are:
Syntax
$( ".selector" ).droppable({
activate: function( event, ui ) {}
});
create(event, ui)
39
JQueryUI
Syntax
$( ".selector" ).droppable({
create: function( event, ui ) {}
});
deactivate(event, ui)
Syntax
$(".selector").droppable(
deactivate: function(event, ui) {}
);
drop(event, ui)
40
JQueryUI
Syntax
$(".selector").droppable(
drop: function(event, ui) {}
);
out(event, ui)
over(event, ui)
41
JQueryUI
Syntax
$(".selector").droppable(
over: function(event, ui) {}
);
Example
The following example demonstrates the event method usage during drop functionality.
This example demonstrates the use of events drop, over and out.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Droppable - Default functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<style>
#draggable-5 {
width: 100px; height: 50px; padding: 0.5em; float: left;
margin: 22px 5px 10px 0;
}
#droppable-8 {
width: 120px; height: 90px;padding: 0.5em; float: left;
margin: 10px;
}
42
JQueryUI
</style>
<script>
$(function() {
$( "#draggable-5" ).draggable();
$("#droppable-8").droppable({
drop: function (event, ui) {
$( this )
.addClass( "ui-state-highlight" )
.find( "p" )
.html( "Dropped!" );
},
over: function (event, ui) {
$( this )
.addClass( "ui-state-highlight" )
.find( "p" )
.html( "moving in!" );
},
out: function (event, ui) {
$( this )
.addClass( "ui-state-highlight" )
.find( "p" )
.html( "moving out!" );
}
});
});
</script>
</head>
<body>
JQueryUI
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you should see the following output:
Drag me to my target
Drop here
Here you will notice how the message in the droppable element changes as you drag the
element.
44
5. JQUERYUI RESIZABLE
JQueryUI
jQueryUI provides resizable() method to resize any DOM element. This method simplifies
the resizing of element which otherwise takes time and lot of coding in HTML. The resizable
() method displays an icon in the bottom right of the item to resize.
Syntax
The resizable() method can be used in two forms:
Syntax
$(selector, context).resizable (options);
You can provide one or more options at a time of using Javascript object. If there are more
than one options to be provided then you will separate them using a comma as follows:
$(selector, context).resizable({option1: value1, option2: value2..... });
Following table lists the different options that can be used with this method:
Option
Description
alsoResize
animate
This option when set to true is used to enable a visual effect during
resizing when the mouse button is released. The default value
isfalse (no effect).
animateDuration
This option is used to set the duration (in milliseconds) of the resizing
effect. This option is used only when animate option is true. By default
its value is "slow".
animateEasing
This option is used to specify which easing to apply when using the
animate option. By default its value is "swing".
45
JQueryUI
aspectRatio
This option is used to indicate whether to keep the aspect (height and
width) ratio for the item. By default its value is false.
autoHide
cancel
containment
delay
disabled
This option disables the resizing mechanism when set to true. The
mouse no longer resizes elements, until the mechanism is enabled,
using the resizable ("enable"). By default its value is false.
distance
With this option, the resizing starts only when the mouse moves a
distance(pixels). By default its value is 1 pixel. This can help prevent
unintended resizing when clicking on an element.
ghost
grid
This option is of type Array [x, y], indicating the number of pixels that
the element expands horizontally and vertically during movement of
the mouse. By default its value is false.
handles
helper
maxHeight
This option is used to set the maximum height the resizable should be
allowed to resize to. By default its value is null.
46
JQueryUI
maxWidth
This option is used to set the maximum width the resizable should be
allowed to resize to. By default its value is null.
minHeight
This option is used to set the minimum height the resizable should be
allowed to resize to. By default its value is 10.
minWidth
This option is used to set the minimum width the resizable should be
allowed to resize to. By default its value is 10.
The following section will show you few a working examples of resize functionality.
Default Functionality
The following example demonstrates a simple example of resizable functionality, passing
no parameters to the resizable() method.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Resizable functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<!-- CSS -->
<style>
.ui-widget-header {
background:#b9cd6d;
border: 1px solid #b9cd6d;
color: #FFFFFF;
font-weight: bold;
}
.ui-widget-content {
background: #cedc98;
border: 1px solid #DDDDDD;
color: #333333;
}
47
JQueryUI
<body>
<!-- HTML -->
<div id="resizable" class="ui-widget-content">
<h3 class="ui-widget-header">Pull my edges to resize me!!</h3>
</div>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you should see the following output. Now, you can play
with the result:
Pull my edges to resize me!!
Drag the square border to resize.
JQueryUI
<script src="[Link]
<!-- CSS -->
<style>
.ui-widget-header {
background:#b9cd6d;
border: 1px solid #b9cd6d;
color: #FFFFFF;
font-weight: bold;
}
.ui-widget-content {
background: #cedc98;
border: 1px solid #DDDDDD;
color: #333333;
}
#resizable-2,#resizable-3 {
width: 150px; height: 150px; padding: 0.5em;
text-align: center; margin: 0; }
</style>
<!-- Javascript -->
<script>
$(function() {
$( "#resizable-2" ).resizable({
animate: true
});
$( "#resizable-3" ).resizable({
ghost: true
});
});
</script>
</head>
<body>
<!-- HTML -->
<div id="resizable-2" class="ui-widget-content">
<h3 class="ui-widget-header">
Pull my edges and Check the animation!!
</h3>
49
JQueryUI
</div><br>
<div id="resizable-3" class="ui-widget-content">
<h3 class="ui-widget-header">I'm ghost!!</h3>
</div>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output. Now, you can
play with the result:
Pull my edges and Check the animation!!
I'm ghost!!
Drag the square border to resize and see the effect of animate and ghost options.
JQueryUI
color: #333333;
}
#container-1 { width: 300px; height: 300px; }
#resizable-4 {background-position: top left;
width: 150px; height: 150px; }
#resizable-4, #container { padding: 0.5em; }
</style>
<script>
$(function() {
$( "#resizable-4" ).resizable({
containment: "#container",
minHeight: 70,
minWidth: 100
});
});
</script>
</head>
<body>
<div id="container" class="ui-widget-content">
<div id="resizable-4" class="ui-state-active">
<h3 class="ui-widget-header">
Resize contained to this container
</h3>
</div>
</div>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you should see the following output. Now, you can play
with the result:
Resize contained to this container
Drag the square border to resize, you cannot resize beyond the main
container.
51
JQueryUI
JQueryUI
$( "#resizable-5" ).resizable({
delay: 1000
});
$( "#resizable-6" ).resizable({
distance: 40
});
$( "#resizable-7" ).resizable({
autoHide: true
});
});
</script>
</head>
<body>
<div id="resizable-5" class="square ui-widget-content">
<h3 class="ui-widget-header">
Resize starts after delay of 1000ms
</h3>
</div><br>
<div id="resizable-6" class="square ui-widget-content">
<h3 class="ui-widget-header">
Resize starts at distance of 40px
</h3>
</div><br>
<div id="resizable-7" class="square ui-widget-content">
<h3 class="ui-widget-header">
Hover over me to see the magnification icon!
</h3>
</div>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you should see the following output. Now, you can play
with the result:
Resize starts after delay of 1000ms
53
JQueryUI
Use of alsoResize
The following example demonstrates the usage of option alsoResize in the resize function
of JqueryUI.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Resizable functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<!-- CSS -->
<style>
.ui-widget-header {
background:#b9cd6d;
border: 1px solid #b9cd6d;
color: #FFFFFF;
font-weight: bold;
}
.ui-widget-content {
background: #cedc98;
border: 1px solid #DDDDDD;
color: #333333;
}
#resizable-8,#resizable-9{ width: 150px; height: 150px;
padding: 0.5em;text-align: center; margin: 0; }
</style>
54
JQueryUI
JQueryUI
$( "#resizable-11" ).resizable({
grid: [50,20]
});
56
JQueryUI
});
</script>
</head>
<body>
<div id="resizable-10" class="square ui-widget-content">
<h3 class="ui-widget-header">
Resize with aspectRatio of 10/3
</h3>
</div>
<div id="resizable-11" class="square ui-widget-content">
<h3 class="ui-widget-header">
Snap me to the grid of [50,20]
</h3>
</div>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you should see the following output. Now, you can play
with the result:
Resize with aspectRatio of 10/3
Snap me to the grid of [50,20]
Drag the square border to resize, the first square box resizes with the aspect ratio of 10 /
3 and the second one resizes with grid of [50,20].
Syntax
$(selector, context).resizable ("action", params);;
57
JQueryUI
The following table lists the different actions that can be used with this method:
Action
destroy
Description
This action destroys the resizable functionality of an element
completely. This will return the element back to its pre-init
state.
Syntax
$( ".selector" ).resizable("destroy");
disable
enable
option( optionName )
option()
option(optionName, value )
This action sets the value of the resizable option with the
specified optionName. This option corresponds to one of
those used with resizable (options).
Syntax
58
JQueryUI
option( options )
widget()
Syntax
var widget = $( ".selector" ).resizable( "widget" );
Example
Now let us see an example using the actions from the above table. The following example
demonstrates the use of destroy() and disable() methods.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Resizable functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<!-- CSS -->
<style>
.ui-widget-header {
background:#b9cd6d;
border: 1px solid #b9cd6d;
color: #FFFFFF;
font-weight: bold;
}
.ui-widget-content {
background: #cedc98;
59
JQueryUI
<body>
<!-- HTML -->
<div id="resizable-12" class="ui-widget-content">
<h3 class="ui-widget-header">I'm disable!!</h3>
</div><br>
<div id="resizable-13" class="ui-widget-content">
<h3 class="ui-widget-header">I'm Destroyed!!</h3>
</div>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you should see the following output:
I'm disable!!
I'm Destroyed!!
You cannot resize the first square box as its disabled and the second square box is
destroyed.
60
JQueryUI
create(event, ui)
Description
This event is triggered when the resizable element is created.
Syntax
$( ".selector" ).resizable({
create: function( event, ui ) {}
});
resize(event, ui)
Syntax
61
JQueryUI
$( ".selector" ).resizable({
resize: function( event, ui ) {}
});
start(event, ui)
Syntax
$( ".selector" ).resizable({
start: function( event, ui ) {}
});
62
JQueryUI
stop(event, ui)
Syntax
$( ".selector" ).resizable({
stop: function( event, ui ) {}
});
Example
The following example demonstrates the event method usage during resize functionality.
This example demonstrates the use of events create, and resize.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
63
JQueryUI
JQueryUI
});
</script>
</head>
<body>
<!-- HTML -->
<div id="resizable-14" class="ui-widget-content">
<h3 class="ui-widget-header">Resize !!</h3>
</div><br>
<span id="resizable-15"></span><br>
<span id="resizable-16"></span>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you should see the following output:
Resize !!
Drag the square box and you will see the output getting displayed on resize event.
65
6. JQUERYUI SELECTABLE
JQueryUI
Syntax
The selectable() method can be used in two forms:
Syntax
$(selector, context).selectable (options);
You can provide one or more options at a time using Javascript object. If there are more
than one options to be provided, then you will separate them using a comma as follows:
$(selector, context).selectable({option1: value1, option2: value2..... });
Following table lists the different options that can be used with this method:
Option
appendTo
Description
This option is tells which element the selection helper (the lasso) should
be appended to. By default its value is body.
Syntax
autoRefresh
This option if set to true, the position and size of each selectable item is
computed at the beginning of a select operation. By default its value istrue.
If you have many items, you may want to set this to false and call the
refresh() method manually.
Syntax
66
JQueryUI
cancel
delay
This option is used to set time in milliseconds and defines when the
selecting should start. This can be used to prevent unwanted selections.
By default its value is 0.
Syntax
$( ".selector" ).selectable({ delay: 150 });
disabled
This option when set to true, disables the selection mechanism. Users
cannot select the elements until the mechanism is restored using the
selectable ("enable") instruction. By default its value is false.
Syntax
$( ".selector" ).selectable({ disabled: true });
distance
This option is the distance (in pixels) the mouse must move to consider
the selection in progress. This is useful, for example, to prevent simple
clicks from being interpreted as a group selection. By default its value is 0.
Syntax
$( ".selector" ).selectable({ distance: 30 });
filter
tolerance
This option specifies which mode to use for testing whether the selection
helper (the lasso) should select an item. By default its value is touch.
This can be of type:
67
JQueryUI
touch: This type indicates the drag rectangle only needs to intersect any
portion of the selectable item.
Syntax
The following section will show you a few working examples of selectable functionality.
Default Functionality
The following example demonstrates a simple example of selectable functionality, passing
no parameters to the selectable() method.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI selectable-1</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<style>
#selectable-1 .ui-selecting { background: #707070 ; }
#selectable-1 .ui-selected { background: #EEEEEE; color: #000000; }
#selectable-1 { list-style-type: none; margin: 0;
padding: 0; width: 20%; }
#selectable-1 li { margin: 3px; padding: 0.4em;
font-size: 16px; height: 18px; }
.ui-widget-content {
background: #cedc98;
border: 1px solid #DDDDDD;
color: #333333;
}
</style>
<script>
68
JQueryUI
$(function() {
$( "#selectable-1" ).selectable();
});
</script>
</head>
<body>
<ol id="selectable-1">
<li class="ui-widget-content">Product 1</li>
<li class="ui-widget-content">Product 2</li>
<li class="ui-widget-content">Product 3</li>
<li class="ui-widget-content">Product 4</li>
<li class="ui-widget-content">Product 5</li>
<li class="ui-widget-content">Product 6</li>
<li class="ui-widget-content">Product 7</li>
</ol>
</body>
</html>
Let's save the above code in an HTML file [Link] and open it in a
standard browser which supports javascript, you should see the following output. Now,
you can play with the result:
Product 1
Product 2
Product 3
Product 4
Product 5
Product 6
Product 7
Try to click on products, use CTRLS key to select multiple products.
JQueryUI
<title>jQuery UI Selectable</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<style>
#selectable-2 .ui-selecting,#selectable-3 .ui-selecting {
background: #707070 ; }
#selectable-2 .ui-selected,#selectable-3 .ui-selected {
background: #EEEEEE; color: #000000; }
#selectable-2,#selectable-3 { list-style-type: none; margin: 0;
padding: 0; width: 20%; }
#selectable-2 li,#selectable-3 li { margin: 3px; padding: 0.4em;
font-size: 16px; height: 18px; }
.ui-widget-content {
background: #cedc98;
border: 1px solid #DDDDDD;
color: #333333;
}
</style>
<script>
$(function() {
$( "#selectable-2" ).selectable({
delay : 1000
});
$( "#selectable-3" ).selectable({
distance : 100
});
});
</script>
</head>
<body>
<h3>Starts after delay of 1000ms</h3>
<ol id="selectable-2">
<li class="ui-widget-content">Product 1</li>
70
JQueryUI
id="selectable-3">
<li class="ui-widget-content">Product 4</li>
<li class="ui-widget-content">Product 5</li>
<li class="ui-widget-content">Product 6</li>
<li class="ui-widget-content">Product 7</li>
</ol>
</body>
</html>
Let's save the above code in an HTML file [Link] and open it in a
standard browser which supports javascript, you should see the following output. Now,
you can play with the result:
Starts after delay of 1000ms
Product 1
Product 2
Product 3
Starts after mouse moves distance of 100px
Product 4
Product 5
Product 6
Product 7
Try to click on products, use CTRL key to select multiple products. You will notice that
selection of the Product 1, Product 2 and Product 3 start after a delay of 1000ms. Selection
of the Product 4, Product 5, Product 6 and Product 7 can't be done individually. The
selection starts only after the mouse moves a distance of 100px.
Use of Filter
The following example demonstrates the usage of two options delay and distance in the
selectable function of JqueryUI.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
71
JQueryUI
<title>jQuery UI selectable-4</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<style>
#selectable-4 .ui-selecting { background: #707070 ; }
#selectable-4 .ui-selected { background: #EEEEEE; color: #000000; }
#selectable-4 { list-style-type: none; margin: 0;
padding: 0; width: 20%; }
#selectable-4 li { margin: 3px; padding: 0.4em;
font-size: 16px; height: 18px; }
.ui-widget-content {
background: #cedc98;
border: 1px solid #DDDDDD;
color: #333333;
}
</style>
<script>
$(function() {
$( "#selectable-4" ).selectable({
filter : "li:first-child"
});
});
</script>
</head>
<body>
<ol id="selectable-4">
<li class="ui-widget-content">Product 1</li>
<li class="ui-widget-content">Product 2</li>
<li class="ui-widget-content">Product 3</li>
<li class="ui-widget-content">Product 4</li>
<li class="ui-widget-content">Product 5</li>
<li class="ui-widget-content">Product 6</li>
<li class="ui-widget-content">Product 7</li>
</ol>
72
JQueryUI
</body>
</html>
Let's save the above code in an HTML file [Link] and open it in a
standard browser which supports javascript, you should see the following output. Now,
you can play with the result:
Product 1
Product 2
Product 3
Product 4
Product 5
Product 6
Product 7
Try to click on products. You will notice that only first product can be selected.
Syntax
$(selector, context).selectable ("action", params);;
Following table lists the different actions that can be used with this method:
Action
destroy
Description
This action removes the selectable functionality of an
element completely. The elements return to their pre-init
state.
Syntax
$( ".selector" ).selectable("destroy");
disable
$( ".selector" ).selectable("disable");
73
JQueryUI
enable
option( optionName )
option()
option( options )
refresh
74
JQueryUI
widget
Syntax
var widget = $( ".selector" ).selectable( "widget" );
Example
Now let us see an example using the actions from the above table. The following example
demonstrates the use of disable() and option(optionName, value) methods.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Selectable</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<style>
#selectable-5 .ui-selecting,#selectable-6 .ui-selecting {
background: #707070 ; }
#selectable-5 .ui-selected,#selectable-6 .ui-selected {
background: #EEEEEE; color: #000000; }
#selectable-5,#selectable-6 {
list-style-type: none; margin: 0; padding: 0; width: 20%; }
#selectable-5 li,#selectable-6 li {
margin: 3px; padding: 0.4em; font-size: 16px; height: 18px; }
.ui-widget-content {
background: #cedc98;
border: 1px solid #DDDDDD;
color: #333333;
}
</style>
<script>
75
JQueryUI
$(function() {
$( "#selectable-5" ).selectable();
$( "#selectable-5" ).selectable('disable');
$( "#selectable-6" ).selectable();
$( "#selectable-6" ).selectable( "option", "distance", 1 );
});
</script>
</head>
<body>
<h3>Disabled using disable() method</h3>
<ol id="selectable-5">
<li class="ui-widget-content">Product 1</li>
<li class="ui-widget-content">Product 2</li>
<li class="ui-widget-content">Product 3</li>
</ol>
<h3>Select using method option( optionName, value )</h3>
<ol
id="selectable-6">
<li class="ui-widget-content">Product 4</li>
<li class="ui-widget-content">Product 5</li>
<li class="ui-widget-content">Product 6</li>
<li class="ui-widget-content">Product 7</li>
</ol>
</body>
</html>
Let's save the above code in an HTML file [Link] and open it in a
standard browser which supports javascript, you should see the following output:
Disabled using disable() method
Product 1
Product 2
Product 3
JQueryUI
Product 6
Product 7
Try to click on products, use CTRL key to select multiple products. You will notice that
Product 1, Product 2, and Product 3 are disabled. Selection of Product 4, Product 5, Product
6 and Product 7 happens after the mouse moves distance of 1px.
create(event, ui)
Description
This event is triggered when the selectable element is created.
Where event is of type Event, and ui is of type Object.
Syntax
$( ".selector" ).selectable({
create: function( event, ui ) {}
});
selected(event, ui)
selecting(event, ui)
77
JQueryUI
$( ".selector" ).selectable({
selecting: function( event, ui ) {}
});
start(event, ui)
stop(event, ui)
unselected(event, ui)
This event is triggered at the end of the select operation for each
element that becomes unselected. Where event is of type Event,
and ui is of type Object.
Possible values of ui are:
Syntax
$( ".selector" ).selectable({
unselected: function( event, ui ) {}
});
78
JQueryUI
unselecting(event, ui)
Example
The following example demonstrates the event method usage during selectable
functionality. This example demonstrates the use of event selected.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI selectable-7</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<style>
#selectable-7 .ui-selecting { background: #707070 ; }
#selectable-7 .ui-selected { background: #EEEEEE; color: #000000; }
#selectable-7 { list-style-type: none; margin: 0;
padding: 0; width: 20%; }
#selectable-7 li { margin: 3px; padding: 0.4em;
font-size: 16px; height: 18px; }
.ui-widget-content {
background: #cedc98;
border: 1px solid #DDDDDD;
color: #333333;
79
JQueryUI
}
.resultarea {
background: #cedc98;
border-top: 1px solid #000000;
border-bottom: 1px solid #000000;
color: #333333;
font-size:14px;
}
</style>
<script>
$(function() {
$( "#selectable-7" ).selectable({
selected: function() {
var result = $( "#result" ).empty();
$( ".ui-selected", this ).each(function() {
var index = $( "#selectable-7 li" ).index( this );
[Link]( " #" + ( index + 1 ) );
});
}
});
});
</script>
</head>
<body>
<h3>Events</h3>
<ol id="selectable-7">
<li class="ui-widget-content">Product 1</li>
<li class="ui-widget-content">Product 2</li>
<li class="ui-widget-content">Product 3</li>
<li class="ui-widget-content">Product 4</li>
<li class="ui-widget-content">Product 5</li>
<li class="ui-widget-content">Product 6</li>
<li class="ui-widget-content">Product 7</li>
</ol>
<span class="resultarea">Selected Product</span>>
<span id=result class="resultarea"></span>
80
JQueryUI
</body>
</html>
Let's save the above code in an HTML file [Link] and open it in a
standard browser which supports javascript, you should see the following output:
Events
Product 1
Product 2
Product 3
Product 4
Product 5
Product 6
Product 7
Selected Product
Try to click on products, use CTRL key to select multiple products. You will notice that the
product number selected is printed at the bottom.
81
7. JQUERYUI SORTABLE
JQueryUI
jQueryUI provides sortable() method to reorder elements in list or grid using the mouse.
This method performs sortability action based upon an operation string passed as the first
parameter.
Syntax
The sortable () method can be used in two forms:
Syntax
$(selector, context).sortable(options);
Following table lists the different options that can be used with this method:
Option
appendTo
Description
This option specifies the element in which the new element
created with [Link] will be inserted during the time of the
move/drag. By default its value is parent.
This can be of type:
82
JQueryUI
Syntax
$(".selector").sortable(
{ appendTo: [Link] }
);
axis
Syntax
$(".selector").sortable(
{ axis: "x" }
);
cancel
Syntax
$(".selector").sortable(
{ cancel: "a,button" }
);
connectWith
$(".selector").sortable(
83
JQueryUI
{ connectWith: "#identifier" }
);
containment
$(".selector").sortable(
{ containment: "parent" }
);
cursor
"crosshair" (across)
"pointer" (hand)
84
JQueryUI
"auto" (default)
"wait" (hourglass)
Syntax
$(".selector").sortable(
{ cursor: "move" }
);
cursorAt
Sets the offset of the dragging helper relative to the mouse cursor.
Coordinates can be given as a hash using a combination of one or
two keys: { top, left, right, bottom }. By default its value is "false".
Syntax
$(".selector").sortable(
{ cursorAt: { left: 5 } }
);
delay
JQueryUI
Syntax
$(".selector").sortable(
{ delay: 150 }
);
disabled
Syntax
$(".selector").sortable(
{ disabled: true }
);
distance
dropOnEmpty
This option if set to false, then items from this sortable can't be
dropped on an empty connect sortable. By default its value is true.
Syntax
$(".selector").sortable(
{ dropOnEmpty: false }
);
forceHelperSize
86
JQueryUI
$(".selector").sortable(
{ forceHelperSize: true }
);
forcePlaceholderSize
This option when set to true, takes into account the size of the
placeholder when an item is moved. This option is only useful
[Link] is initialized. By default its value is false.
Syntax
$(".selector").sortable(
{ forcePlaceholderSize: true }
);
grid
handle
$(".selector").sortable(
{ handle: ".handle" }
);
helper
String: If set to "clone", then the element will be cloned and the
clone will be dragged.
87
JQueryUI
Syntax
$(".selector").sortable(
{ helper: "clone" }
);
items
opacity
$(".selector").sortable(
{ opacity: 0.5 }
);
placeholder
revert
88
JQueryUI
Syntax
$(".selector").sortable(
{ revert: true }
);
scroll
scrollSensitivity
This option indicates how many pixels the mouse must exit the
visible area to cause scrolling. By default its value is 20. This
option is used only with [Link] set to true.
Syntax
$(".selector").sortable(
{ scrollSensitivity: 10 }
);
scrollSpeed
This option indicates the scrolling speed of the display once the
scrolling begins. By default its value is 20.
Syntax
$(".selector").sortable(
{ scrollSpeed: 40 }
);
tolerance
This option is a String that specifies which mode to use for testing
whether the item being moved is hovering over another item. By
default its value is "intersect".
intersect: The item overlaps the other item by at least 50%.
Syntax
89
JQueryUI
$(".selector").sortable(
{ tolerance: "pointer" }
);
This option represents z-index for element/helper while being
sorted. By default its value is 1000.
zIndex
Syntax
$(".selector").sortable(
{ zIndex: 9999 }
);
The following section will show you a few working examples of drag functionality.
Default Functionality
The following example demonstrates a simple example of sortable functionality, passing
no parameters to the sortable() method .
<!DOCTYPE html>
<head>
<title>jQuery UI Sortable - Example</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<style>
#sortable-1 { list-style-type: none; margin: 0;
padding: 0; width: 25%; }
#sortable-1 li { margin: 0 3px 3px 3px; padding: 0.4em;
padding-left: 1.5em; font-size: 17px; height: 16px; }
.default {
background: #cedc98;
border: 1px solid #DDDDDD;
color: #333333;
}
</style>
90
JQueryUI
<script>
$(function() {
$( "#sortable-1" ).sortable();
});
</script>
</head>
<body>
<ul id="sortable-1">
<li class="default">Product 1</li>
<li class="default">Product 2</li>
<li class="default">Product 3</li>
<li class="default">Product 4</li>
<li class="default">Product 5</li>
<li class="default">Product 6</li>
<li class="default">Product 7</li>
</ul>
</body>
</html>
Let's save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you should see the following output. Now, you can play
with the result:
Product 1
Product 2
Product 3
Product 4
Product 5
Product 6
Product 7
Re-arrange the products above, use mouse to drag items.
JQueryUI
JQueryUI
<ul id="sortable-3">
<li class="default">Product 1</li>
<li class="default">Product 2</li>
<li class="default">Product 3</li>
<li class="default">Product 4</li>
</ul>
</body>
</html>
Let's save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you should see the following output. Now, you can play
with the result:
Delay by 500ms
Product 1
Product 2
Product 3
Product 4
Use of Placeholder
The following example demonstrates the usage of three option placeholder in the sort
function of JqueryUI.
<!DOCTYPE html>
<head>
<title>jQuery UI Sortable - Example</title>
<link href="[Link] rel="stylesheet">
93
JQueryUI
<script src="[Link]
<script src="[Link]
<style>
#sortable-4 { list-style-type: none; margin: 0;
padding: 0; width: 25%; }
#sortable-4 li { margin: 0 3px 3px 3px; padding: 0.4em;
padding-left: 1.5em; font-size: 17px; height: 16px; }
.highlight {
border: 1px solid red;
font-weight: bold;
font-size: 45px;
background-color: #333333;
}
.default {
background: #cedc98;
border: 1px solid #DDDDDD;
color: #333333;
}
</style>
<script>
$(function() {
$( "#sortable-4" ).sortable({
placeholder: "highlight"
});
});
</script>
</head>
<body>
<ul id="sortable-4">
<li class="default">Product 1</li>
<li class="default">Product 2</li>
<li class="default">Product 3</li>
<li class="default">Product 4</li>
<li class="default">Product 5</li>
<li class="default">Product 6</li>
<li class="default">Product 7</li>
94
JQueryUI
</ul>
</body>
</html>
Let's save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you should see the following output. Now, you can play
with the result:
Product 1
Product 2
Product 3
Product 4
Product 5
Product 6
Product 7
Try to drag items to rearrange them, while you're dragging items, the placeholder (we
have used highlight class to style this space) will show up on an available place.
JQueryUI
color: #333333;
}
</style>
<script>
$(function() {
$( "#sortable-5, #sortable-6" ).sortable({
connectWith: "#sortable-5, #sortable-6"
});
$( "#sortable-7").sortable({
connectWith: "#sortable-5",
dropOnEmpty: false
});
});
</script>
</head>
<body>
<ul id="sortable-5"><h3>List 1</h3>
<li class="default">A</li>
<li class="default">B</li>
<li class="default">C</li>
<li class="default">D</li>
</ul>
<ul id="sortable-6"><h3>List 2</h3>
<li class="default">a</li>
<li class="default">b</li>
<li class="default">c</li>
<li class="default">d</li>
</ul>
<ul id="sortable-7"><h3>List 3</h3>
<li class="default">e</li>
<li class="default">f</li>
<li class="default">g</li>
<li class="default">h</li>
</ul>
</body>
96
JQueryUI
</html>
Let's save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you should see the following output. Now, you can play
with the result:
List 1
A
B
C
D
List 2
a
b
c
d
List 3
e
f
g
h
Sort items from one List1 into another (List2) and vice versa, by passing a selector into
theconnectWith option. This is done by grouping all related lists with a CSS class, and then
pass that class into the sortable function (i.e., connectWith: '#sortable-5, #sortable-6').
Try to drag the items under List 3 to the List 2 or List 1. As we have
set dropOnEmpty option to false, it won't be possible to drop these items.
Syntax
$(selector, context).sortable ("action", [params]);
97
JQueryUI
The following table lists the actions for this method:
Action
cancel()
Description
destroy()
disable()
enable()
option( optionName )
JQueryUI
var
isDisabled
$(
".selector"
).sortable(
"option",
"disabled" );
option()
option( optionName,
value )
option( options )
refresh()
toArray( options )
99
JQueryUI
serialize( options )
refreshPositions()
widget()
Example
Now let us see an example using the actions from the above table. The following example
demonstrates the use of toArray( options ) method.
<!DOCTYPE html>
<head>
<title>jQuery UI Sortable - Example</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<style>
#sortable-8{ list-style-type: none; margin: 0;
padding: 0; width: 25%; float:left;}
#sortable-8 li{ margin: 0 3px 3px 3px; padding: 0.4em;
padding-left: 1.5em; font-size: 17px; height: 16px; }
.default {
background: #cedc98;
100
JQueryUI
JQueryUI
Description
activate(event, ui)
sender: The sortable that the item comes from if moving from
one sortable to another.
Syntax
$( ".selector" ).sortable({
activate: function( event, ui ) {}
});
102
JQueryUI
beforeStop(event,
ui)
sender: The sortable that the item comes from if moving from
one sortable to another.
Syntax
$( ".selector" ).sortable({
beforeStop: function( event, ui ) {}
});
change(event, ui)
JQueryUI
sender: The sortable that the item comes from if moving from
one sortable to another.
Syntax
$( ".selector" ).sortable({
beforeStop: function( event, ui ) {}
});
create(event, ui)
$( ".selector" ).sortable({
create: function( event, ui ) {}
});
deactivate(event, ui)
104
JQueryUI
sender: The sortable that the item comes from if moving from
one sortable to another.
Syntax
$( ".selector" ).sortable({
deactivate: function( event, ui ) {}
});
out(event, ui)
This event is triggered when the sort item is moved away from a
connected list. Possible values of ui are:
105
JQueryUI
sender: The sortable that the item comes from if moving from
one sortable to another.
Syntax
$( ".selector" ).sortable({
out: function( event, ui ) {}
});
over(event, ui)
sender: The sortable that the item comes from if moving from
one sortable to another.
106
JQueryUI
Syntax
$( ".selector" ).sortable({
over: function( event, ui ) {}
});
receive(event, ui)
sender: The sortable that the item comes from if moving from
one sortable to another.
Syntax
107
JQueryUI
$( ".selector" ).sortable({
receive: function( event, ui ) {}
});
remove(event, ui)
sender: The sortable that the item comes from if moving from
one sortable to another.
Syntax
$( ".selector" ).sortable({
remove: function( event, ui ) {}
});
108
JQueryUI
sort(event, ui)
sender: The sortable that the item comes from if moving from
one sortable to another.
Syntax
$( ".selector" ).sortable({
sort: function( event, ui ) {}
});
start(event, ui)
109
JQueryUI
sender: The sortable that the item comes from if moving from
one sortable to another.
Syntax
$( ".selector" ).sortable({
start: function( event, ui ) {}
});
stop(event, ui)
110
JQueryUI
sender: The sortable that the item comes from if moving from
one sortable to another.
Syntax
$( ".selector" ).sortable({
stop: function( event, ui ) {}
});
update(event, ui)
sender: The sortable that the item comes from if moving from
one sortable to another.
111
JQueryUI
Syntax
$( ".selector" ).sortable({
update: function( event, ui ) {}
});
Example
The following example demonstrates the event method usage during drop functionality.
This example demonstrates the use of events receive, start and stop.
<!DOCTYPE html>
<head>
<title>jQuery UI Sortable - Example</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<style>
#sortable-10, #sortable-11 { list-style-type: none;
margin: 0; padding: 0; width: 80%; }
#sortable-10 li, #sortable-11 li { margin: 0 3px 3px 3px;
padding: 0.4em; padding-left: 1.5em;
font-size: 17px; height: 16px; }
.highlight {
border: 1px solid #000000;
font-weight: bold;
font-size: 45px;
background-color: #cedc98;
}
.default {
background: #cedc98;
112
JQueryUI
JQueryUI
<div class="wrap1">
<h3>List 1</h3>
<ul id="sortable-10">
<li class="default">A</li>
<li class="default">B</li>
<li class="default">C</li>
<li class="default">D</li>
</ul>
</div>
<div class="wrap1">
<h3>List 2</h3>
<ul id="sortable-11">
<li class="default">a</li>
<li class="default">b</li>
<li class="default">c</li>
<li class="default">d</li>
</ul>
</div>
</div>
<hr />
<span id=result></span>
</body>
</html>
Let's save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you should see the following output. Now, you can play
with the result:
List 1
A
B
C
D
114
JQueryUI
List 2
a
b
c
d
Try sorting the items in List 1, you will see the message getting displayed at
the start andstop of event. Now drop items from List 2 to List 1, again a message gets
displayed on thereceive event.
115
JQueryUI
116
8. JQUERYUI ACCORDION
JQueryUI
Accordion Widget in jQueryUI is a jQuery based expandable and collapsible content holder
that is broken into sections and probably looks like tabs. jQueryUI provides aunitccordion()
method to achieve this.
Syntax
The accordion() method can be used in two forms:
Syntax
$(selector, context).accordion (options);
You can provide one or more options at a time using Javascript object. If there are more
than one options to be provided then you will separate them using a comma as follows:
$(selector, context).accordion({option1: value1, option2: value2..... });
Following table lists the different options that can be used with this method:
Option
Description
Indicates the index of the menu that is open when the page is first accessed.
By default its value is 0, i.e the first menu.
Option - active
active
Indicates the index of the menu that is open when the page is first accessed.
By default its value is 0, i.e the first menu.
Boolean: If set to false will collapse all panels. This requires the collapsible
option to be true.
117
JQueryUI
Integer: The zero-based index of the panel that is active (open). A negative
value selects panels going backward from the last panel.
Syntax
$( ".selector" ).accordion(
{ active: 2 }
);
This option is used to set how to animate changing panels. By default its
value is {}.
Option - animate
This option is used to set how to animate changing panels. By default its
value is {}.
animate
Syntax
$( ".selector" ).accordion(
{ animate: "bounceslide" }
);
118
JQueryUI
This option when set to true, it allows users to close a menu by clicking on it.
By default, clicks on the open panels header have no effect. By default its
value is false.
Option - collapsible
This option when set to true, it allows users to close a menu by clicking on it.
By default, clicks on the open panels header have no effect. By default its
value is false.
collapsible
Syntax
$( ".selector" ).accordion(
{ collapsible: true }
);
This option when set to true disables the accordion. By default its value
isfalse.
Option - disabled
This option when set to true disables the accordion. By default its value is
false.
disabled
Syntax
$( ".selector" ).accordion(
{ disabled: true }
);
This option specifies the event used to select an accordion header. By default
its value is click.
event
Option - event
119
JQueryUI
This option specifies the event used to select an accordion header. By default
its value is click.
Syntax
$( ".selector" ).accordion(
{ event: "mouseover" }
);
This option specifies a selector or element to override the default pattern for
identifying the header elements. By default its value is > li > :first-child,>
:not(li):even.
Option - header
This option specifies a selector or element to override the default pattern for
identifying the header elements. By default its value is > li > :first-child,>
:not(li):even.
header
Syntax
$( ".selector" ).accordion(
{ header: "h3" }
);
This option is used to control the height of accordion and panels. By default
its value is auto.
Option - heightStyle
This option is used to control the height of accordion and panels. By default
auto: All panels will be set to the height of the tallest panel.
120
JQueryUI
fill: Expand to the available height based on the accordion's parent height.
Syntax
$( ".selector" ).accordion(
{ heightStyle: "fill" }
);
This option is an object that defines the icons to use to the left of the header
text for opened and closed panels. The icon to use for closed panels is
specified as a property named header, whereas the icon to use for open
panels is specified as a property named headerSelected. By default its value
is { "header": "ui-icon-triangle-1-e", "activeHeader": "ui-icon-triangle-1-s" }.
Option - icons
icons
This option is an object that defines the icons to use to the left of the header
text for opened and closed panels. The icon to use for closed panels is
specified as a property named header, whereas the icon to use for open
panels is specified as a property named headerSelected. By default its value
is { "header": "ui-icon-triangle-1-e", "activeHeader": "ui-icon-triangle-1-s" }.
Syntax
$( ".selector" ).accordion(
{ icons: { "header": "ui-icon-plus", "activeHeader": "ui-icon-minus" } }
);
The following section will show you a few working examples of accordion widget functionality.
Default Functionality
The following example demonstrates a simple example of accordion widget functionality,
passing no parameters to the accordion () method.
121
JQueryUI
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Accordion Example </title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<script>
$(function() {
$( "#accordion-1" ).accordion();
});
</script>
<style>
#accordion-1{font-size: 14px;}
</style>
</head>
<body>
<div id="accordion-1">
<h3>Tab 1</h3>
<div>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco
laboris nisi ut aliquip ex ea commodo consequat.
</p>
</div>
<h3>Tab 2</h3>
<div>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua.
122
JQueryUI
</div>
</div>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a
standard browser which supports javascript, you must also see the following output. Now,
you can play with the result:
Tab 1
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
Tab 2
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
Tab 3
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
Click headers(Tab 1,Tab 2,Tab 3) to expand/collapse content that is broken into logical
sections, much like tabs.
123
JQueryUI
Use of collapsible
The following example demonstrates the usage of three options collapsible in the
accordion widget of JqueryUI.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Accordion Example </title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<script>
$(function() {
$( "#accordion-2" ).accordion({
collapsible: true
});
});
</script>
<style>
#accordion-2{font-size: 14px;}
</style>
</head>
<body>
<div id="accordion-2">
<h3>Tab 1</h3>
<div>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
</p>
</div>
<h3>Tab 2</h3>
<div>
124
JQueryUI
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
</p>
</div>
<h3>Tab 3</h3>
<div>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
</p>
<ul>
<li>List item one</li>
<li>List item two</li>
<li>List item three</li>
</ul>
</div>
</div>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a
standard browser which supports javascript, you must also see the following output. Now,
you can play with the result:
Tab 1
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
Tab 2
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
125
JQueryUI
Tab 3
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
Here we have set collapsible to true. Click on an accordion header, this allows collapsing
the active section.
Use of Heightstyle
The following example demonstrates the usage of three options heightStyle in the
accordion widget of JqueryUI.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Accordion Example </title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<script>
$(function() {
$( "#accordion-3" ).accordion({
heightStyle: "content"
});
$( "#accordion-4" ).accordion({
heightStyle: "fill"
});
});
</script>
<style>
#accordion-3, #accordion-4{font-size: 14px;}
</style>
</head>
<body>
126
JQueryUI
<h3>Height style-content</h3>
<div style="height:250px">
<div id="accordion-3">
<h3>Tab 1</h3>
<div>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing
elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua.
</p>
<ul>
<li>List item one</li>
<li>List item two</li>
<li>List item three</li>
<li>List item four</li>
<li>List item five</li>
</ul>
</div>
<h3>Tab 2</h3>
<div>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing
elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua.
</p>
</div>
<h3>Tab 3</h3>
<div>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing
elit,
sed do eiusmod tempor incididunt ut labore et dolore
magna aliqua.
</p>
</div>
</div>
127
JQueryUI
</div><br><br>
<h3>Height style-Fill</h3>
<div style="height:250px">
<div id="accordion-4">
<h3>Tab 1</h3>
<div>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing
elit, sed do eiusmod tempor incididunt ut labore
et dolore magna aliqua.
</p>
<ul>
<li>List item one</li>
<li>List item two</li>
<li>List item three</li>
<li>List item four</li>
<li>List item five</li>
</ul>
</div>
<h3>Tab 2</h3>
<div>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing
elit, sed do eiusmod tempor incididunt ut labore
et dolore magna aliqua.
</p>
</div>
<h3>Tab 3</h3>
<div>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing
elit, sed do eiusmod tempor incididunt ut labore
et dolore magna aliqua.
</p>
</div>
</div>
128
JQueryUI
</div>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a
standard browser which supports javascript, you must also see the following output. Now,
you can play with the result:
Height style-content
Tab 1
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.
List
List
List
List
List
item
item
item
item
item
one
two
three
four
five
Tab 2
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.
Tab 3
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.
Height style-Fill
Tab 1
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.
List
List
List
List
List
item
item
item
item
item
one
two
three
four
five
Tab 2
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.
Tab 3
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.
129
JQueryUI
Here we have two accordions, the first one has heightStyle option set to content, which
allows the accordion panels to keep their native height. Second accordion
has heightStyleoption set to fill, the script will automatically set the dimensions of the
accordion to the height of its parent container.
Syntax
$(selector, context).accordion ("action", params);;
Following table lists the different actions that can be used with this method:
Action
destroy
Description
This action destroys the accordion functionality of an
element completely. The elements return to their pre-init
state.
Action - destroy
This action destroys the accordion functionality of an
element completely. The elements return to their pre-init
state.
Syntax
$(".selector").accordion("destroy");
disable
Syntax
$(".selector").accordion("disable");
enable
130
JQueryUI
Syntax
$(".selector").accordion("enable");
option( optionName )
Syntax
var isDisabled = $( ".selector" ).accordion( "option",
"disabled" );
option
Syntax
var options = $( ".selector" ).accordion( "option" );
option(optionName,
value)
Syntax
$( ".selector" ).accordion( "option", "disabled", true );
option( options )
131
JQueryUI
Syntax
$( ".selector" ).accordion( "option", { disabled: true } );
refresh
Syntax
$(".selector").accordion("refresh");
widget
Action - widget
This action returns the accordion widget element; the one
annotated with the ui-accordion class name.
Syntax
var widget = $( ".selector" ).accordion( "widget" );
Example
Now let us see an example using the actions from the above table. The following example
demonstrates the use of option( optionName, value ) method.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Accordion Example </title>
132
JQueryUI
JQueryUI
</div>
<h3>Tab 2</h3>
<div>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing
elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud
exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat.
</p>
</div>
<h3>Tab 3</h3>
<div>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing
elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip
ex ea commodo consequat.
</p>
<ul>
<li>List item one</li>
<li>List item two</li>
<li>List item three</li>
</ul>
</div>
</div>
<div style="margin-top:30px">
<input type="radio" name="disable" id="disableaccordion"
value="disable">Disable accordion
<input type="radio" name="disable" id="enableaccordion"
checked
value="enable">Enable accordion
</div>
</body>
134
JQueryUI
</html>
Let us save the above code in an HTML file [Link] and open it in a
standard browser which supports javascript, you must also see the following output:
Tab 1
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
Tab 2
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
Tab 3
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
Here we demonstrate enabling and disabling of the accordions. Select the respective radio
buttons to check each action.
activate(event, ui)
Description
This event is triggered when a menu is activated. This event is
only fired on panel activation, it is not fired for the initial panel
when the accordion widget is created.
Event - activate(event, ui)
This event is triggered when a menu is activated. This event is
only fired on panel activation, it is not fired for the initial panel
when the accordion widget is created. Where event is of
typeEvent, and ui is of type Object. Possible values of ui are:
newHeader: A jQuery object representing the header that was
just activated.
135
JQueryUI
Syntax
$( ".selector" ).accordion({
activate: function( event, ui ) {}
});
beforeActivate(event,
ui)
Syntax
$( ".selector" ).accordion({
beforeActivate: function( event, ui ) {}
});
create(event, ui)
136
JQueryUI
Syntax
$( ".selector" ).accordion({
create: function( event, ui ) {}
});
Example
The following example demonstrates the event method usage in accordion widgets. This
example demonstrates the use of events create, beforeActive and active.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Accordion Example </title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<script>
$(function() {
$( "#accordion-6" ).accordion({
create: function (event, ui) {
$("span#result").html ($("span#result").html () +
"<b>Created</b><br>");
},
beforeActivate : function (event, ui)
{
$("span#result").html ($("span#result").html () + ",
<b>beforeActivate</b><br>");
},
activate: function (event, ui) {
137
JQueryUI
$("span#result").html ($("span#result").html () +
"<b>activate</b><br>");
}
});
});
</script>
<style>
#accordion-6{font-size: 14px;}
</style>
</head>
<body>
<div id="accordion-6">
<h3>Tab 1</h3>
<div>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore
magna aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat.
</p>
</div>
<h3>Tab 2</h3>
<div>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore
magna aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat.
</p>
</div>
<h3>Tab 3</h3>
<div>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit,
138
JQueryUI
Tab 1
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
Tab 2
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
Tab 3
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
139
9. JQUERYUI AUTOCOMPLETE
JQueryUI
Auto completion is a mechanism frequently used in modern websites to provide the user
with a list of suggestions for the beginning of the word, which he/she has typed in a text
box. The user can then select an item from the list, which will be displayed in the input
field. This feature prevents the user from having to enter an entire word or a set of words.
JQueryUI provides an autocomplete widget - a control that acts a lot like a <select>
dropdown, but filters the choices to present only those that match what the user is typing
into a control. jQueryUI provides the autocomplete() method to create a list of
suggestions below the input field and adds new CSS classes to the elements concerned to
give them the appropriate style.
Any field that can receive
namely,
<input>elements,
the contenteditable attribute.
into
and
an Autocomplete,
elements
with
Syntax
The autocomplete() method can be used in two forms:
Syntax
$(selector, context).autocomplete (options);
You can provide one or more options at a time using Javascript object. If there are more
than one options to be provided then you will separate them using a comma as follows:
$(selector, context).autocomplete({option1: value1, option2: value2..... });
Following table lists the different options that can be used with this method:
Option
Description
appendTo
This option is used append an element to the menu. By default its value
isnull.
Syntax
140
JQueryUI
This option when set to true, the first item of the menu will automatically be
focused when the menu is shown. By default its value is false.
autoFocus Syntax
delay
Syntax
$( ".selector" ).autocomplete({ disabled: true });
The number of characters that must be entered before trying to obtain the
matching values (as specified by the source option). This can prevent too
large a value set from being presented when a few characters isnt enough
minLength to whittle the set down to a reasonable level. By default its value is 1.
Syntax
$( ".selector" ).autocomplete({ minLength: 0 });
position
This option identifies the position of the suggestions menu in relation to the
associated input element. The of option defaults to the input element, but
you can specify another element to position against. By default its value is {
my: "left top", at: "left bottom", collision: "none" }.
Syntax
141
JQueryUI
String representing the URL of a server resource that will return matching
data,
source
An array of local data from which the value will be matched, or a function
that serves as a general callback from providing the matching values.
Syntax
The following section will show you a few working examples of autocomplete widget
functionality.
Default Functionality
The following example demonstrates a simple example of autocomplete widget
functionality, passing no parameters to the autocomplete() method .
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Autocomplete functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<!-- Javascript -->
<script>
$(function() {
var availableTutorials = [
142
JQueryUI
"ActionScript",
"Boostrap",
"C",
"C++",
];
$( "#automplete-1" ).autocomplete({
source: availableTutorials
});
});
</script>
</head>
<body>
<!-- HTML -->
<div class="ui-widget">
<p>Type "a" or "s"</p>
<label for="automplete-1">Tags: </label>
<input id="automplete-1">
</div>
</body>
</html>
Let's save the above code in an HTML file [Link] and open it in a
standard browser which supports javascript, you should see the following output. Now,
you can play with the result:
Type "a" or "s"
Tags:
Use of autoFocus
The following example demonstrates the usage of option autoFocus in the autocomplete
widget of JqueryUI.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Autocomplete functionality</title>
143
JQueryUI
Tags:
144
JQueryUI
JQueryUI
});
});
</script>
</head>
<body>
<!-- HTML -->
<div class="ui-widget">
<p>Type two letter for e.g:ja,sc etc</p>
<label for="automplete-3">Tags: </label>
<input id="automplete-3">
</div>
</body>
</html>
Let's save the above code in an HTML file [Link] and open it in a
standard browser which supports javascript, you must also see the following output. Now,
you can play with the result:
Type two letter for e.g:ja,sc etc
Tags:
Use of Label
The following example demonstrates the usage of option label in the autocomplete widget
of JqueryUI.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Autocomplete functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<!-- Javascript -->
<script>
$(function() {
$( "#autocomplete-4" ).autocomplete({
146
JQueryUI
source: [
{ label: "India", value: "IND" },
{ label: "Australia", value: "AUS" }
]
});
});
</script>
</head>
<body>
<!-- HTML -->
<div class="ui-widget">
<p>Type I OR A</p>
<input id="autocomplete-4">
</div>
</body>
</html>
Let's save the above code in an HTML file [Link] and open it in a
standard browser which supports javascript, you must also see the following output. Now,
you can play with the result:
Type I OR A
JQueryUI
$( "#autocomplete-5" ).autocomplete({
source: "/jqueryui/[Link]",
minLength: 2
});
</script>
</head>
<body>
<input id="autocomplete-5">
</body>
</html>
The file [Link] is placed at the same location as
([Link]). Contents of [Link] are as below:
the
above
file
<?
$term = $_GET[ "term" ];
$companies = array(
array( "label" => "JAVA", "value" => "1" ),
array( "label" => "DATA IMAGE PROCESSING", "value" => "2" ),
array( "label" => "JAVASCRIPT", "value" => "3" ),
array( "label" => "DATA MANAGEMENT SYSTEM", "value" => "4" ),
array( "label" => "COMPUTER PROGRAMMING", "value" => "5" ),
array( "label" => "SOFTWARE DEVELOPMENT LIFE CYCLE", "value" => "6" ),
array( "label" => "LEARN COMPUTER FUNDAMENTALS", "value" => "7" ),
array( "label" => "IMAGE PROCESSING USING JAVA", "value" => "8" ),
array( "label" => "CLOUD COMPUTING", "value" => "9" ),
array( "label" => "DATA MINING", "value" => "10" ),
array( "label" => "DATA WAREHOUSE", "value" => "11" ),
array( "label" => "E-COMMERCE", "value" => "12" ),
array( "label" => "DBMS", "value" => "13" ),
array( "label" => "HTTP", "value" => "14" )
);
$result = array();
foreach ($companies as $company) {
$companyLabel = $company[ "label" ];
148
JQueryUI
Syntax
$(selector, context).autocomplete ("action", params);;
Following table lists the different actions that can be used with this method:
Action
Description
This action hides the list of suggestions in the Autocomplete
menu. This method does not accept any arguments.
close
Syntax
$( ".selector" ).autocomplete("close");
destroy
149
JQueryUI
disable
enable
option( optionName )
Syntax
var isDisabled = $( ".selector" ).autocomplete( "option",
"disabled" );
This action gets an object containing key/value pairs
representing the current autocomplete options hash.
option
Syntax
var options = $( ".selector" ).autocomplete( "option" );
option( options )
search( [value ] )
JQueryUI
Syntax
$( ".selector" ).autocomplete( "search", "" );
Retrieve the <ul> DOM element corresponding to the list of
suggestions. This is an object of jQuery class that allows easy
access to the list without using jQuery selectors.
widget
Syntax
$( ".selector" ).autocomplete("widget");
Example
Now let us see an example using the actions from the above table. The following example
demonstrates the use of option( optionName, value ) method.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Autocomplete functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<!-- Javascript -->
<script>
$(function() {
var availableTutorials = [
"ActionScript",
"Boostrap",
"C",
"C++",
"Ecommerce",
"Jquery",
"Groovy",
"Java",
"JavaScript",
"Lua",
"Perl",
151
JQueryUI
"Ruby",
"Scala",
"Swing",
"XHTML"
];
$( "#automplete-6" ).autocomplete({
source: availableTutorials
});
$( "#automplete-6" ).autocomplete("option", "position", { my :
"right-10 top+10", at: "right top" })
});
</script>
</head>
<body>
<!-- HTML -->
<div class="ui-widget">
<p>Type "a" or "s"</p>
<label for="automplete-6">Tags: </label>
<input id="automplete-6">
</div>
</body>
</html>
Let's save the above code in an HTML file [Link] and open it in a
standard browser which supports javascript, you must also see the following output:
Type "a" or "s"
Tags:
Extension Points
The autocomplete widget can be extended as its built with the widget factory. When
extending widgets, you have the ability to override or add to the behavior of existing
methods. The following table lists methods that act as extension points with the same API
stability as the plugin methods listed above.
Method
_renderItem(
item )
Description
ul,
JQueryUI
_renderMenu(
items )
_resizeMenu()
ul,
Description
This event is triggered when the value of the <input> element is
changed based upon a selection. When triggered, this event will
always come after the close event is triggered.
Possible values of ui are:
change(event, ui)
item: The item selected from the menu, if any. Otherwise the
property is null.
Syntax
$( ".selector" ).autocomplete({
change: function( event, ui ) {}
});
This event is triggered whenever the autocomplete menu closes.
Syntax
close(event, ui)
$( ".selector" ).autocomplete({
close: function( event, ui ) {}
});
This event is triggered when the autocomplete is created.
Syntax
create(event, ui)
$( ".selector" ).autocomplete({
create: function( event, ui ) {}
});
focus(event, ui)
JQueryUI
Syntax
$( ".selector" ).autocomplete({
focus: function( event, ui ) {}
});
This event is triggered after the data has been readied and the
menu is about to open.
Syntax
open(event, ui)
$( ".selector" ).autocomplete({
open: function( event, ui ) {}
});
$( ".selector" ).autocomplete({
response: function( event, ui ) {}
});
154
JQueryUI
This event is triggered after any delay and minLength criteria have
been met, just before the mechanism specified by source is
activated. If canceled, the search operation is aborted. Where
event is of type Event, and ui is of type Object.
search(event, ui)
Syntax
$( ".selector" ).autocomplete({
search: function( event, ui ) {}
});
This event is triggered when a value is selected from the
autocomplete menu. Cancelling this event prevents the value from
being set into the <input> element (but doesnt prevent the menu
from closing). Where event is of type Event, and ui is of type
Object. Possible values of ui are:
select(event, ui)
item: An Object with label and value properties for the selected
option.
Syntax
$( ".selector" ).autocomplete({
select: function( event, ui ) {}
});
Example
The following example demonstrates the event method usage in autocomplete widget.
This example demonstrates the use of events focus, and select.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Autocomplete functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
155
JQueryUI
<script src="[Link]
<style>
#project-label {
display: block;
font-weight: bold;
margin-bottom: 1em;
}
#project-icon {
float: left;
height: 32px;
width: 32px;
}
#project-description {
margin: 0;
padding: 0;
}
</style>
<!-- Javascript -->
<script>
$(function() {
var projects = [
{
value: "java",
label: "Java",
desc: "write once run anywhere",
},
{
value: "jquery-ui",
label: "jQuery UI",
desc: "the official user interface library for jQuery",
},
{
value: "Bootstrap",
label: "Twitter Bootstrap",
desc: "popular front end frameworks ",
}
156
JQueryUI
];
$( "#project" ).autocomplete({
minLength: 0,
source: projects,
focus: function( event, ui ) {
$( "#project" ).val( [Link] );
return false;
},
select: function( event, ui ) {
$( "#project" ).val( [Link] );
$( "#project-id" ).val( [Link] );
$( "#project-description" ).html( [Link] );
return false;
}
})
.data( "ui-autocomplete" )._renderItem = function( ul, item ) {
return $( "<li>" )
.append( "<a>" + [Link] + "<br>" + [Link] + "</a>" )
.appendTo( ul );
};
});
</script>
</head>
<body>
<div id="project-label">Select a project (type "a" for a start):</div>
<input id="project">
<input type="hidden" id="project-id">
<p id="project-description"></p>
</body>
</html>
157
JQueryUI
Let's save the above code in a HTML file [Link] and open it in a
standard browser which supports javascript. you must also see the following output:
Select a project (type "a" for a start):
158
JQueryUI
jQueryUI provides button() method to transform the HTML elements (like buttons, inputs
and anchors) into themeable buttons, with automatic management of mouse movements
on them, all managed transparently by jQuery UI.
In order to group radio buttons, Button also provides an additional widget,
calledButtonset. Buttonset is used by selecting a container element (which contains the
radio buttons) and calling .buttonset().
Syntax
The button() method can be used in two forms:
Syntax
$(selector, context).button (options);
You can provide one or more options at a time using Javascript object. If there are more
than one options to be provided then you will separate them using a comma as follows:
Description
This option deactivates the button is set to true. By default its value is false.
disabled
Syntax
icons
This option specifies that one or two icons are to be displayed in the button:
primary icons to the left, secondary icons to the right. The primary icon is
identified by the primary property of the object, and the secondary icon is
identified by the secondary property. By default its value is primary: null,
secondary: null.
159
JQueryUI
Syntax
$( ".selector" ).button({ icons: { primary: "ui-icon-gear", secondary: "ui-icontriangle-1-s" } });
label
This option specifies text to display on the button that overrides the natural
label. If omitted, the natural label for the element is displayed. In the case of
radio buttons and checkboxes, the natural label is the <label> element
associated with the control. By default its value is null.
Syntax
$( ".selector" ).button({ label: "custom label" });
text
Syntax
$(selector, context).button ("action", params);
The following table lists the different actions that can be used with this method:
Action
Description
destroy
disable
enable
option( optionName )
160
JQueryUI
option
option( optionName, value This action sets the value of the button option associated with
)
the specified optionName.
option( options )
refresh
widget
Description
This event is triggered when the button is created. Where event is
of type Event, and ui is of type Object.
Syntax
create(event, ui)
$( ".selector" ).button({
create: function( event, ui ) {}
});
Example
The following example demonstrates the event method usage for button widget
functionality. This example demonstrates the use of event create.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Buttons functionality</title>
161
JQueryUI
162
JQueryUI
Datepickers in jQueryUI allow users to enter dates easily and visually. You can customize
the date format and language, restrict the selectable date ranges and add in buttons and
other navigation options easily.
jQueryUI provides datepicker() method that creates a datepicker and changes the
appearance of HTML elements on a page by adding new CSS classes. Transforms the
<input>, <div>, and <span> elements in the wrapped set into a datepicker control.
By default, for <input> elements, the datepicker calendar opens in a small overlay when
the associated text field gains focus. For an inline calendar, simply attach the datepicker
to a <div>, or <span> element.
Syntax
The datepicker() method can be used in two forms:
Syntax
$(selector, context).datepicker(options);
You can provide one or more options at a time using Javascript object. If there are more
than one options to be provided then you will separate them using a comma as follows:
$(selector, context).datepicker({option1: value1, option2: value2..... });
The following table lists the different options that can be used with this method:
Option
altField
Description
This option specifies a jQuery selector for a field that is also
updated with any date selections. The altFormat option can be
used to set the format for this value. This is quite useful for
setting date values into a hidden input element to be submitted
to the server, while displaying a more user-friendly format to
the user. By default its value is "".
Syntax
163
JQueryUI
$(".selector").datepicker(
{ altField: "#actualDate" }
);
This option is used when an altField option is specified. It
provides the format for the value to be written to the alternate
element. By default its value is "".
altFormat
Syntax
$(".selector").datepicker(
{ altFormat: "yy-mm-dd" }
);
This option is a String value to be placed after the <input>
element, intended to show instructions to the user. By default
its value is "".
Syntax
appendText
$(".selector").datepicker(
{ appendText: "(yyyy-mm-dd)" }
);
This option when set to true resizes the <input> element to
accommodate the datepickers date format as set with the
dateFormat option. By default its value is false.
Syntax
autoSize
$(".selector").datepicker(
{ autoSize: true }
);
beforeShow
beforeShowDay
JQueryUI
buttonImage
buttonImageOnly
buttonText
calculateWeek
changeMonth
changeYear
closeText
constrainInput
This option if set true (the default), text entry into the <input>
element is constrained to characters allowed by the
currentdateformat option. By default its value is true.
currentText
165
JQueryUI
dateFormat
dayNames
dayNamesMin
dayNamesShort
defaultDate
This option sets the initial date for the control, overriding the
default value of today, if the <input> element has no value.
This can be a Date instance, the number of days from today,
or astring specifying an absolute or relative date. By default its
value is null.
This option specifies the speed of the animation that makes the
datepicker appear. Can be one of slow, normal, or fast, or the
number of milliseconds for the animation to span. By default
its value is normal.
duration
Syntax
$(".selector").datepicker(
{ duration: "slow" }
);
This option specifies which day is considered the first day of
the week, and will be displayed as the left-most column. By
default its value is 0.
firstDay
Syntax
$(".selector").datepicker(
{ firstDay: 1 }
);
166
JQueryUI
This option when set to true, the current day link is set to the
selected date, overriding the default of today. By default its
value is false.
gotoCurrent
Syntax
$(".selector").datepicker(
{ gotoCurrent: true }
);
This option if set to true, hides the next and previous links (as
opposed to merely disabling them) when they arent
applicable,
as
determined
by
the
settings
of
the minDate and maxDateoptions. By default its value is false.
hideIfNoPrevNext
Syntax
$(".selector").datepicker(
{ hideIfNoPrevNext: true }
);
This option when set to true, specifies the localizations as a
right-to-left language. By default its value is false.
Syntax
isRTL
$(".selector").datepicker(
{ isRTL: true }
);
This option sets the maximum selectable date for the control.
This can be a Date instance, the number of days from today,
or a string specifying an absolute or relative date. By default
its value is null.
Syntax
maxDate
$(".selector").datepicker(
{ maxDate: "+1m +1w" }
);
minDate
This option sets the minimum selectable date for the control.
This can be a Date instance, the number of days from today,
or a string specifying an absolute or relative date. By default
its value is null.
167
JQueryUI
Syntax
$(".selector").datepicker(
{ minDate: new Date(2007, 1 - 1, 1) }
);
This option is a 12-element array providing the full month
names with the 0th element representing January. Can be used
to localize the control. By default its value is [ "January",
"February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December" ].
Syntax
monthNames
$(".selector").datepicker(
{ monthNames: [ "janvier", "fvrier", "mars", "avril", "mai",
"juin", "juillet", "aot", "septembre", "octobre", "novembre",
"dcembre" ] }
);
This option specifies a 12-element array providing the short
month names with the 0th element representing January. Can
be used to localize the control. By default its value is [ "Jan",
"Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
"Nov", "Dec" ].
monthNamesShort
Syntax
$(".selector").datepicker(
{ monthNamesShort: [ "jan", "fv", "mar", "avr", "mai",
"Jui", "Jul", "ao", "sep", "Oot", "nov", "dc" ] }
);
navigationAsDateFormat
$(".selector").datepicker(
168
JQueryUI
{ navigationAsDateFormat: true }
);
This option specifies the text to replace the default caption of
Next for the next month navigation link. ThemeRoller replaces
this text with an icon. By default its value is Next.
nextText
Syntax
$(".selector").datepicker(
{ nextText: "Later" }
);
This option specifies the number of months to show in the
datepicker. By default its value is 1.
Syntax
numberOfMonths
$(".selector").datepicker(
{ numberOfMonths: [ 2, 3 ] }
);
onChangeMonthYear
onClose
onSelect
prevText
169
JQueryUI
selectOtherMonths
shortYearCutoff
Syntax
$(".selector").datepicker(
{ shortYearCutoff: 50 }
);
This option specifies sets the name of the animation to be used
to show and hide the datepicker. If specified, must be one
ofshow (the default), fadeIn, slideDown, or any of the jQuery
UI show/hide animations. By default its value is show.
showAnim
Syntax
$(".selector").datepicker(
{ showAnim: "fold" }
);
This option if set to true, a button panel at the bottom of the
datepicker is displayed, containing current and close buttons.
The caption of these buttons can be provided via
thecurrentText and closeText options. By default its value
is false.
Syntax
showButtonPanel
$(".selector").datepicker(
{ showButtonPanel: true }
);
showCurrentAtPos
JQueryUI
Syntax
$(".selector").datepicker(
{ showCurrentAtPos: 3 }
);
This option specifies if set to true, the positions of the month
and year are reversed in the header of the datepicker. By
default its value is false.
Syntax
showMonthAfterYear
$(".selector").datepicker(
{ showMonthAfterYear: true }
);
This option specifies when the datepicker should appear. The
possible values are focus, button or both. By default its value
isfocus.
showOn
Syntax
$(".selector").datepicker(
{ showOn: "both" }
);
This option provides a hash to be passed to the animation when
a jQuery UI animation is specified for the showAnimoption. By
default its value is {}.
Syntax
showOptions
$(".selector").datepicker(
{ showOptions: { direction: "up" } }
);
showOtherMonths
This option if set to true, dates before or after the first and last
days of the current month are displayed. These dates aren't
selectable unless the selectOtherMonths option is also set to
true. By default its value is false.
Syntax
171
JQueryUI
$(".selector").datepicker(
{ showOtherMonths: true }
);
This option if set to true, the week number is displayed in a
column to the left of the month display. The calculateWeek
option can be used to alter the manner in which this value is
determined. By default its value is false.
showWeek
Syntax
$(".selector").datepicker(
{ showWeek: true }
);
This option specifies specifies how many months to move when
one of the month navigation controls is clicked. By default its
value is 1.
Syntax
stepMonths
$(".selector").datepicker(
{ stepMonths: 3 }
);
This option specifies the text to display for the week number
column, overriding the default value of Wk, when showWeek is
true. By default its value is Wk.
weekHeader
Syntax
$(".selector").datepicker(
{ weekHeader: "W" }
);
yearRange
JQueryUI
{ yearRange: "2002:2012" }
);
This option displays additional text after the year in the
datepicker header. By default its value is "".
Syntax
yearSuffix
$(".selector").datepicker(
{ yearSuffix: "CE" }
);
The following section will show you a few working examples of datepicker functionality.
Default Functionality
The following example demonstrates a simple example of datepicker functionality passing
no parameters to the datepicker() method .
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
JQueryUI
</html>
Let us save the above code in an HTML file [Link] and open it in a
standard browser which supports javascript, you must also see the following output. Now,
you can play with the result:
Enter Date:
Inline Datepicker
The following example demonstrates a simple example of inline datepicker functionality.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<!-- Javascript -->
<script>
$(function() {
$( "#datepicker-2" ).datepicker();
});
</script>
</head>
<body>
<!-- HTML -->
Enter Date: <div id="datepicker-2"></div>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a
standard browser which supports javascript, you must also see the following output. Now,
you can play with the result:
Enter Start Date:
In the above example we use <div> element to get the inline date picker.
174
JQueryUI
Enter Date:
Alternate Date:
175
JQueryUI
In the above example, you can see that, the date formate for first input is set as yy-mmdd. If you select some date from datepicker, the same date is reflected in the second input
field whose date format is set as "DD, MM, yy".
Use of beforeshowday
The following example shows the usage of option beforeShowDay in the datepicker
function of JqueryUI.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
JQueryUI
Let us save the above code in an HTML file [Link] and open it in a
standard browser which supports javascript, you must also see the following output. Now,
you can play with the result:
Enter Date:
In the above example sunday and saturday are disabled.
177
JQueryUI
Let us save the above code in an HTML file [Link] and open it in a
standard browser which supports javascript, you must also see the following output. Now,
you can play with the result:
Enter Date:
In the above example an icon is displayed which needs to b clicked to open the datepicker.
JQueryUI
Enter Date:
In the above example the names of the days are changed using dayNamesMin. You can
also see a default date is set.
JQueryUI
JQueryUI
</script>
</head>
<body>
<!-- HTML -->
<p>Enter Date: <input type="text" id="datepicker-10"></p>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a
standard browser which supports javascript, you must also see the following output. Now,
you can play with the result:
Enter Date:
In the above example you can see dropdown menus for Month and Year fields. And we are
dispalying 4 months in an array structure of [2,2].
181
JQueryUI
</script>
</head>
<body>
<!-- HTML -->
<p>Enter Date: <input type="text" id="datepicker-11"></p>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a
standard browser which supports javascript, you must also see the following output. Now,
you can play with the result:
Enter Date:
In the above example, you can see that, week numbers are displayed on the left side of
datepicker as showWeek is set to true. The year is have a suffix of "-CE".
Syntax
$(selector, context).datepicker ("action", [params]);
The following table lists the actions for this method:
Action
destroy()
Description
This action removes the datepicker functionality completely.
This will return the element back to its pre-init state. This
method does not accept any arguments.
hide()
182
JQueryUI
isDisabled()
option( optionName )
option()
option(
value )
optionName, This action sets the value of the datepicker option associated
with the specified optionName.
option( options )
refresh()
This action redraws the date picker, after having made some
external modifications. This method does not accept any
arguments.
setDate( date )
This action sets the specified date as the current date of the
datepicker.
show()
widget()
The following examples show the use of some of the actions listed in the above table.
183
JQueryUI
Enter Date:
JQueryUI
Enter Date:
185
JQueryUI
Dialog boxes are one of the nice ways of presenting information on an HTML page. A dialog
box is a floating window with a title and content area. This window can be moved, resized,
and of course, closed using "X" icon by default.
jQueryUI provides dialog() method that transforms the HTML code written on the page
into HTML code to display a dialog box.
Syntax
The dialog() method can be used in two forms:
Syntax
$(selector, context).dialog(options);
You can provide one or more options at a time using Javascript object. If there are more
than one options to be provided then you will separate them using a comma as follows:
Description
appendTo
If this option is set to false, it will prevent the ui-draggable class from
being added in the list of selected DOM elements. By default its value
is true.
autoOpen
This option unless set to false, the dialog box is opened upon creation.
When false, the dialog box will be opened upon a call to dialog('open').
By default its value is true.
buttons
This option adds buttons in the dialog box. These are listed as objects,
and each property is the text on the button. The value is a callback
186
JQueryUI
function called when the user clicks the button. By default its value
is{}.
closeOnEscape
Unless this option set to false, the dialog box will be closed when the
user presses the Escape key while the dialog box has focus. By default
its value is true.
closeText
This option contains text to replace the default of Close for the close
button. By default its value is "close".
dialogClass
If this option is set to false, it will prevent the ui-draggable class from
being added in the list of selected DOM elements. By default its value
is "".
draggable
Unless you this option to false, dialog box will be draggable by clicking
and dragging the title bar. By default its value is true.
height
This option sets the height of the dialog box. By default its value
is"auto".
hide
This option is used to set the effect to be used when the dialog box is
closed. By default its value is null.
maxHeight
This option sets maximum height, in pixels, to which the dialog box can
be resized. By default its value is false.
maxWidth
This option sets the maximum width to which the dialog can be resized,
in pixels. By default its value is false.
minHeight
This option is the minimum height, in pixels, to which the dialog box
can be resized. By default its value is 150.
minWidth
This option is the minimum width, in pixels, to which the dialog box can
be resized. By default its value is 150.
modal
If this option is set to true, the dialog will have modal behavior; other
items on the page will be disabled, i.e., cannot be interacted with.
Modal dialogs create an overlay below the dialog but above other page
elements. By default its value is false.
position
This option specifies the initial position of the dialog box. Can be one
of the predefined positions: center (the default), left, right, top, or
bottom. Can also be a 2-element array with the left and top values (in
187
JQueryUI
resizable
This option unless set to false, the dialog box is resizable in all
directions. By default its value is true.
show
This option is an effect to be used when the dialog box is being opened.
By default its value is null.
title
This option specifies the text to appear in the title bar of the dialog box.
By default its value is null.
width
This option specifies the width of the dialog box in pixels. By default its
value is 300.
The following section will show you a few working examples of dialog functionality.
Default Functionality
The following example demonstrates a simple example of dialog functionality passing no
parameters to the dialog() method .
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Dialog functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<!-- CSS -->
<style>
.ui-widget-header,.ui-state-default, ui-button{
background:#b9cd6d;
border: 1px solid #b9cd6d;
color: #FFFFFF;
font-weight: bold;
}
</style>
188
JQueryUI
JQueryUI
<script src="[Link]
<script src="[Link]
<!-- CSS -->
<style>
.ui-widget-header,.ui-state-default, ui-button{
background:#b9cd6d;
border: 1px solid #b9cd6d;
color: #FFFFFF;
font-weight: bold;
}
</style>
<!-- Javascript -->
<script>
$(function() {
$( "#dialog-2" ).dialog({
autoOpen: false,
buttons: {
OK: function() {$(this).dialog("close");}
},
title: "Success",
position: {
my: "left center",
at: "left center"
}
});
$( "#opener-2" ).click(function() {
$( "#dialog-2" ).dialog( "open" );
});
});
</script>
</head>
<body>
<!-- HTML -->
<div id="dialog-2" title="Dialog Title goes here...">This my first jQuery
UI Dialog!</div>
<button id="opener-2">Open Dialog</button>
190
JQueryUI
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output. Now, you can
play with the result:
This my first jQuery UI Dialog!
Open Dialog
JQueryUI
show : "slide",
height: 200
});
$( "#opener-3" ).click(function() {
$( "#dialog-3" ).dialog( "open" );
});
});
</script>
</head>
<body>
<!-- HTML -->
<div id="dialog-3" title="Dialog Title goes here...">This my first jQuery
UI Dialog!</div>
<button id="opener-3">Open Dialog</button>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output. Now, you can
play with the result:
This my first jQuery UI Dialog!
Open Dialog
Use of Modal
The following example demonstrates the usage of three options buttons, title and
position in the dialog widget of JqueryUI.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Dialog functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<!-- CSS -->
192
JQueryUI
<style>
.ui-widget-header,.ui-state-default, ui-button{
background:#b9cd6d;
border: 1px solid #b9cd6d;
color: #FFFFFF;
font-weight: bold;
}
</style>
<!-- Javascript -->
<script>
$(function() {
$( "#dialog-4" ).dialog({
autoOpen: false,
modal: true,
buttons: {
OK: function() {$(this).dialog("close");}
},
});
$( "#opener-4" ).click(function() {
$( "#dialog-4" ).dialog( "open" );
});
});
</script>
</head>
<body>
<!-- HTML -->
<div id="dialog-4" title="Dialog Title goes here...">This my first jQuery
UI Dialog!</div>
<button id="opener-4">Open Dialog</button>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco
laboris nisi ut aliquip ex ea commodo consequat.
</p>
JQueryUI
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output. Now, you can
play with the result:
This my first jQuery UI Dialog!
Open Dialog
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
Enter Name:
Syntax
$(selector, context).dialog ("action", [params]);
The following table lists the actions for this method:
Action
Description
close()
This action closes the dialog box. This method does not
accept any arguments.
destroy()
isOpen()
moveToTop()
JQueryUI
open()
This action opens the dialog box. This method does not
accept any arguments.
option( optionName )
option()
option( options )
widget()
Example
Now let us see an example using the actions from the above table. The following example
demonstrates the use of isOpen(), open() and close() methods.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Dialog functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<!-- CSS -->
<style>
.ui-widget-header,.ui-state-default, ui-button{
background:#b9cd6d;
border: 1px solid #b9cd6d;
color: #FFFFFF;
font-weight: bold;
195
JQueryUI
}
</style>
<!-- Javascript -->
<script type="text/javascript">
$(function(){
$("#toggle").click(function() {
Event Method
beforeClose(event, ui)
Description
This event is triggered when the dialog box is about to close.
Returning false prevents the dialog box from closing. It is
196
JQueryUI
close(event, ui)
create(event, ui)
drag(event, ui)
dragStart(event, ui)
dragStop(event, ui)
focus(event, ui)
open(event, ui)
resize(event, ui)
resizeStart(event, ui)
resizeStop(event, ui)
focus.
The following examples demonstrate the use of the events listed in the above table.
197
JQueryUI
JQueryUI
199
JQueryUI
JQueryUI
}
});
$( "#opener-6" ).click(function() {
$( "#dialog-7" ).dialog( "open" );
});
});
</script>
</head>
<body>
<div id="dialog-7" title="Dialog Title goes here...">Resize this dialog
to see the dialog co-ordinates in the title!</div>
<button id="opener-6">Open Dialog</button>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output:
Open Dialog
Resize this dialog to see the dialog co-ordinates in the title!
Extension Points
The dialog widget is built with the widget factory and can be extended. To extend widgets,
we can either override or add to the behavior of existing methods. Following method
provides as extension point with the same API stability as the dialog methods.
Method
Description
_allowInteraction(event)
The jQuery UI Widget Factory is an extensible base on which all of jQuery UI's widgets are
built. Using the widget factory to build a plugin provides conveniences for state
management, as well as conventions for common tasks like exposing plugin methods and
changing options after instantiation.
201
JQueryUI
A menu widget usually consists of a main menu bar with pop up menus. Items in pop up
menus often have sub pop up menus. A menu can be created using the markup elements
as long as the parent-child relation is maintained (using <ul> or <ol>). Each menu item
has an anchor element.
The Menu Widget in jQueryUI can be used for inline and popup menus, or as a base for
building more complex menu systems. For example, you can create nested menus with
custom positioning.
jQueryUI provides menu() methods to create a menu.
Syntax
The menu() method can be used in two forms
Syntax
$(selector, context).menu (options);
You can provide one or more options at a time using Javascript object. If there are more
than one options to be provided then you will separate them using a comma as follows:
$(selector, context).menu({option1: value1, option2: value2..... });
The following table lists the different options that can be used with this method:
Option
disabled
icons
Description
This option if set to true disables the menu. By default its value is false.
This option sets the icons for submenus. By default its value is { submenu:
"ui-icon-carat-1-e" }.
202
JQueryUI
menus
This option is a selector for the elements that serve as the menu container,
including sub-menus. By default its value is ul.
position
This option sets the position of submenus in relation to the associated parent
menu item. By default its value is { my: "left top", at: "right top" }.
role
This option is used to customize the ARIA roles used for the menu and menu
items. By default its value is menu.
Default Functionality
The following example demonstrates a simple example of menu widget functionality,
passing no parameters to the menu() method .
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Menu functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<!-- CSS -->
<style>
.ui-menu {
width: 200px;
}
</style>
<!-- Javascript -->
<script>
$(function() {
$( "#menu-1" ).menu();
});
</script>
</head>
203
JQueryUI
<body>
<!-- HTML -->
<ul id="menu-1">
<li><a href="#">Spring</a></li>
<li><a href="#">Hibernate</a></li>
<li><a href="#">Java</a>
<ul>
<li><a href="#">Java IO</a></li>
<li><a href="#">Swing</a></li>
<li><a href="#">Jaspr Reports</a></li>
</ul>
</li>
<li><a href="#">JSF</a></li>
<li><a href="#">HTML5</a></li>
</ul>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output. Now, you can
play with the result:
Spring
Hibernate
Java
o
Java IO
Swing
Jaspr Reports
JSF
HTML5
In the above example you can see a themeable menu with mouse and keyboard
interactions for navigation.
JQueryUI
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Menu functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<!-- CSS -->
<style>
.ui-menu {
width: 200px;
}
</style>
<!-- Javascript -->
<script>
$(function() {
$( "#menu-2" ).menu({
icons: { submenu: "ui-icon-circle-triangle-e"},
position: { my: "right top", at: "right-10 top+5" }
});
});
</script>
</head>
<body>
<!-- HTML -->
<ul id="menu-2">
<li><a href="#">Spring</a></li>
<li><a href="#">Hibernate</a></li>
<li><a href="#">Java</a>
<ul>
<li><a href="#">Java IO</a></li>
<li><a href="#">Swing</a></li>
<li><a href="#">Jaspr Reports</a></li>
</ul>
</li>
205
JQueryUI
<li><a href="#">JSF</a></li>
<li><a href="#">HTML5</a></li>
</ul>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output. Now, you can
play with the result:
Spring
Hibernate
Java
o
Java IO
Swing
Jaspr Reports
JSF
HTML5
In the above example you can see we have applied an icon image for the submenu list
and also changed the submenu position.
Syntax
$(selector, context).menu ("action", params);;
The following table lists the different actions that can be used with this method:
Action
blur( [event ] )
Description
This action removes the focus from a menu. It triggers the
menu's blur event by resetting any active element style.
Where event is of type Event and represents what triggered
the menu to blur.
206
JQueryUI
collapse( [event ] )
destroy()
disable()
This action disables the menu. This method does not accept
any arguments.
enable()
This action enables the the menu. This method does not
accept any arguments.
expand( [event ] )
isFirstItem()
isLastItem()
next( [event ] )
This action delegates the active state to the next menu item.
Where event is of type Event and represents what triggered
the focus to move.
nextPage( [event ] )
This action moves active state to first menu item below the
bottom of a scrollable menu or the last item if not scrollable.
Where event is of type Event and represents what triggered
the focus to move.
207
JQueryUI
option( optionName )
option()
This action sets the value of the menu option associated with
the specified optionName. Where optionName is of
type String and represents name of option to set and valueis
of type Object and represents value to set for the option.
option( options )
previous( [event ] )
previousPage( [event ] )
This action moves active state to first menu item above the
top of a scrollable menu or the first item if not scrollable.
Where event is of type Event and represents what triggered
the focus to move.
refresh()
select( [event ] )
widget()
The following examples demonstrate how to use the actions given in the above table.
208
JQueryUI
JQueryUI
</li>
<li><a href="#">JSF</a></li>
<li><a href="#">HTML5</a></li>
</ul>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output:
Spring
Hibernate
Java
o
Java IO
Swing
Jaspr Reports
JSF
HTML5
In the above example, you can see that, the menu is disabled.
JQueryUI
Spring
Hibernate
JSF
HTML5
Java
211
JQueryUI
Java IO
Swing
Jaspr Reports
In the above example you can see the focus is on the last menu item. Now expand the
submenu and when the mouse leaves the submenu, the submenu is closed.
Description
blur(event, ui)
create(event, ui)
focus(event, ui)
select(event, ui)
Example
The following example demonstrates the event method usage for menu widget
functionality. This example demonstrates the use of event create, blur and focus.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Menu functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<!-- CSS -->
<style>
.ui-menu {
212
JQueryUI
width: 200px;
}
</style>
<!-- Javascript -->
<script>
$(function() {
$( "#menu-5" ).menu({
create: function( event, ui ) {
var result = $( "#result" );
[Link]( "Create event<br>" );
},
blur: function( event, ui ) {
var result = $( "#result" );
[Link]( "Blur event<br>" );
},
focus: function( event, ui ) {
var result = $( "#result" );
[Link]( "focus event<br>" );
}
});
});
</script>
</head>
<body>
<!-- HTML -->
<ul id="menu-5">
<li><a href="#">Spring</a></li>
<li><a href="#">Hibernate</a></li>
<li><a href="#">JSF</a></li>
<li><a href="#">HTML5</a></li>
<li><a href="#">Java</a>
<ul>
<li><a href="#">Java IO</a></li>
<li><a href="#">Swing</a></li>
<li><a href="#">Jaspr Reports</a></li>
</ul>
213
JQueryUI
</li>
</ul>
<span id="result"></span>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output:
Spring
Hibernate
JSF
HTML5
Java
o
Java IO
Swing
Jaspr Reports
In the above example, we are printing the messages based on the events triggered.
214
JQueryUI
Syntax
The progressbar() method can be used in two forms:
Syntax
$(selector, context).progressbar (options);
You can provide one or more options at a time using Javascript object. If there are more
than one options to be provided then you will separate them using a comma as follows:
$(selector, context).progressbar({option1: value1, option2: value2..... });
The following table lists the different options that can be used with this method:
Option
Description
disabled
This option when set to true disables the progress bars. By default its value
isfalse.
215
JQueryUI
max
This option sets the maximum value for a progress bar. By default its value
is100.
value
This option specifies the initial value of the progress bar. By default its value
is 0.
The following section will show you a few working examples of progressbar functionality.
Default Functionality
The following example demonstrates a simple example of progressbar functionality,
passing no parameters to the progressbar() method .
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI ProgressBar functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<style>
.ui-widget-header {
background: #cedc98;
border: 1px solid #DDDDDD;
color: #333333;
font-weight: bold;
}
</style>
<script>
$(function() {
$( "#progressbar-1" ).progressbar({
value: 30
});
});
</script>
</head>
216
JQueryUI
<body>
<div id="progressbar-1"></div>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a
standard browser which supports javascript, you must also see the following output. Now,
you can play with the result:
This example shows the creation of progress bar using of progressbar() method. This is a
default determinate progress bar.
JQueryUI
});
function progress() {
var val = [Link]( "value" ) || 0;
[Link]( "value", val + 1 );
if ( val < 99 ) {
setTimeout( progress, 100 );
}
}
setTimeout( progress, 3000 );
});
</script>
</head>
<body>
<div id="progressbar-2"></div>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a
standard browser which supports javascript, you must also see the following output. Now,
you can play with the result:
Here you can see that the progress bar fills from right to left and stops when the value
reaches 300.
Syntax
$(selector, context).progressbar ("action", params);;
The following table lists the different actions that can be used with this method:
Action
destroy
Description
This action removes the progress bar functionality of an
element completely. The elements return to their pre-init
state. This method does not accept any arguments.
218
JQueryUI
destroy
disable
enable
option( optionName )
option
option( options )
This action sets one or more options for the progress bars.
The argument options is a map of option-value pairs to be
set.
value
value( value )
widget
Example
Now let us see an example using the actions from the above table. The following example
demonstrates the use of disable() and option( optionName, value ) methods.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
219
JQueryUI
JQueryUI
<h3>Disabled Progressbar</h3>
<div id="progressbar-3"></div><br>
<h3>Progressbar with max value set</h3>
<div id="progressbar-4"></div>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a
standard browser which supports javascript, you must also see the following output:
DISABLED PROGRESS BAR
Description
change(event, ui)
complete(event, ui)
create(event, ui)
Example
The following example demonstrates the event method usage during progressbar
functionality. This example demonstrates the use of events change and complete.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI ProgressBar functionality</title>
221
JQueryUI
JQueryUI
if ( val < 99 ) {
setTimeout( progress, 100 );
}
}
setTimeout( progress, 3000 );
});
</script>
</head>
<body>
<div id="progressbar-5">
<div class="progress-label">
Loading...
</div>
</div>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a
standard browser which supports javascript, you must also see the following output:
Here you can see as the progressbar changes its changed value is printed and upon
complete event the "Loading Completed!" message displays.
223
JQueryUI
A slider is used whenever a numeric value within a certain range is to be obtained. The
advantage of a slider over text input is that it becomes impossible for the user to enter a
bad value. Any value that they can pick with the slider is valid.
jQueryUI provides us a slider control through slider widget. jQueryUI provides slider()
method changes the appearance of HTML elements in the page, adding new CSS classes
that give them the appropriate style.
Syntax
The slider () method can be used in two forms:
Syntax
$(selector, context).slider (options);
You can provide one or more options at a time using Javascript object. If there are more than one
options to be provided then you will separate them using a comma as follows:
$(selector, context).slider({option1: value1, option2: value2..... });
The following table lists the different options that can be used with this method:
Option
Description
animate
This option when set to true, creates an animated effect when users click
directly on the axis. By default its value is false.
disabled
This option when set to true, disables the slider. By default its value isfalse.
max
This option specifies the upper value of the range that the slider can attain
the value represented when the handle is moved to the far right (for
horizontal sliders) or top (for vertical sliders). By default its value is 100.
224
JQueryUI
min
This option specifies the lower value of the range that the slider can attain
the value represented when the handle is moved to the far left (for
horizontal sliders) or bottom (for vertical sliders). By default its value is 0.
orientation
range
This option specifies whether the slider represents a range. By default its
value is false.
step
This option specifies discrete intervals between the minimum and maximum
values that the slider is allowed to represent. By default its value is 1.
value
This option specifies the initial value of a single-handle slider. If there are
multiple handles (see the values options), specifies the value for the first
handle. By default its value is 1.
values
This option is of type Array and causes multiple handles to be created and
specifies the initial values for those handles. This option should be an array
of possible values, one for each handle. By default its value is null.
The following section will show you a few working examples of slider functionality.
Default Functionality
The following example demonstrates a simple example of slider functionality, passing no
parameters to the slider() method.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Slider functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
JQueryUI
$( "#slider-1" ).slider();
});
</script>
</head>
<body>
<!-- HTML -->
<div id="slider-1"></div>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output. Now, you can
play with the result:
In the above example, it is a basic horizontal slider and has a single handle that can be
moved with the mouse or by using the arrow keys.
(a)
value
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Slider functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<!-- Javascript -->
<script>
$(function() {
$( "#slider-2" ).slider({
value: 60,
animate:"slow",
orientation: "horizontal"
});
});
</script>
226
JQueryUI
</head>
<body>
<!-- HTML -->
<div id="slider-2"></div>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output. Now, you can
play with the result:
In the above example the value of slider i.e the initial value is set as 60, hence you see
the handle at initial value of 60. Now just click directly on the axis and see the animation
effect.
(a)
range,
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Slider functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<!-- Javascript -->
<script>
$(function() {
$( "#slider-3" ).slider({
range:true,
min: 0,
max: 500,
values: [ 35, 200 ],
slide: function( event, ui ) {
$( "#price" ).val( "$" + [Link][ 0 ] + " - $" + [Link][
1 ] );
}
227
JQueryUI
});
$( "#price" ).val( "$" + $( "#slider-3" ).slider( "values", 0 ) +
" - $" + $( "#slider-3" ).slider( "values", 1 ) );
});
</script>
</head>
<body>
<!-- HTML -->
<p>
<label for="price">Price range:</label>
<input type="text" id="price"
style="border:0; color:#b9cd6d; font-weight:bold;">
</p>
<div id="slider-3"></div>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output. Now, you can
play with the result:
Price range:
In the above example we have set the range option to true to capture a range of values
with two drag handles. The space between the handles is filled with a different background
color to indicate those values are selected.
Syntax
$(selector, context).slider ("action", params);;
228
JQueryUI
The following table lists the different actions that can be used with this method:
Action
Description
destroy
disable
enable
option( optionName )
option()
This action sets the value of the slider option associated with
the specified optionName. The argument optionNameis
name of the option to be set and value is the value to be set
for the option.
option( options )
This action sets one or more options for the slider. The
argument options is a map of option-value pairs to be set.
value
value( value )
values
values( index )
229
JQueryUI
values( values )
widget
Example
Now let us see an example using the actions from the above table. The following example
demonstrates the use of disable() and value() method.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Slider functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<!-- Javascript -->
<script>
$(function() {
$( "#slider-4" ).slider({
orientation:"vertical"
});
$( "#slider-4" ).slider('disable');
$( "#slider-5" ).slider({
orientation:"vertical",
value:50,
slide: function( event, ui ) {
$( "#minval" ).val( [Link] );
}
});
$( "#minval" ).val( $( "#slider-5" ).slider( "value" ) );
});
230
JQueryUI
</script>
</head>
<body>
<!-- HTML -->
<div id="slider-4"></div>
<p>
<label for="minval">Minumum value:</label>
<input type="text" id="minval"
style="border:0; color:#b9cd6d; font-weight:bold;">
</p>
<div id="slider-5"></div>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output:
Minumum value:
In the above example, the first slider is disabled and the second slider the value is set to
50.
Description
change(event, ui)
create(event, ui)
slide(event, ui)
start(event, ui)
stop(event, ui)
JQueryUI
Example
The following example demonstrates the event method usage during slider functionality.
This example demonstrates the use of events start, stop, change and slide.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Slider functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<!-- Javascript -->
<script>
$(function() {
$( "#slider-6" ).slider({
range:true,
min: 0,
max: 500,
values: [ 35, 200 ],
start: function( event, ui ) {
$( "#startvalue" )
.val( "$" + [Link][ 0 ] + " - $" + [Link][ 1 ] );
},
stop: function( event, ui ) {
$( "#stopvalue" )
.val( "$" + [Link][ 0 ] + " - $" + [Link][ 1 ] );
},
change: function( event, ui ) {
$( "#changevalue" )
.val( "$" + [Link][ 0 ] + " - $" + [Link][ 1 ] );
},
slide: function( event, ui ) {
$( "#slidevalue" )
.val( "$" + [Link][ 0 ] + " - $" + [Link][ 1 ] );
}
232
JQueryUI
});
});
</script>
</head>
<body>
<!-- HTML -->
<div id="slider-6"></div>
<p>
<label for="startvalue">Start:</label>
<input type="text" id="startvalue"
style="border:0; color:#b9cd6d; font-weight:bold;">
</p>
<p>
<label for="stopvalue">Stop:</label>
<input type="text" id="stopvalue"
style="border:0; color:#b9cd6d; font-weight:bold;">
</p>
<p>
<label for="changevalue">Change:</label>
<input type="text" id="changevalue"
style="border:0; color:#b9cd6d; font-weight:bold;">
</p>
<p>
<label for="slidevalue">Slide:</label>
<input type="text" id="slidevalue"
style="border:0; color:#b9cd6d; font-weight:bold;">
</p>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output:
Start:
Stop:
233
JQueryUI
Change:
Slide:
234
JQueryUI
Spinner widget adds a up/down arrow to the left of a input box thus allowing a user to
increment/decrement a value in the input box. It allows users to type a value directly, or
modify an existing value by spinning with the keyboard, mouse or scrollwheel. It also has
a step feature to skip values. In addition to the basic numeric features, it also enables
globalized formatting options (ie currency, thousand separator, decimals, etc.) thus
providing a convenient internationalized masked entry box.
The following example depends on Globalize. You can get the Globalize files from
[Link] Click the releases link, select the version you want,
and download the .zip or [Link] file. Extract the files and copy the following files to the
folder where your example is located.
lib/[Link] : This file contains the Javascript code for dealing with
localizations
lib/[Link] : This file contains a complete set of the locales that the
Globalize library comes with.
These files are also present in the external folder of your jquery-ui library.
jQueryUI provides spinner() method which creates a spinner.
Syntax
The spinner() method can be used in two forms:
Syntax
$(selector, context).spinner (options);
You can provide one or more options at a time using Javascript object. If there are more
than one options to be provided then you will separate them using a comma as follows:
$(selector, context).spinner({option1: value1, option2: value2..... });
235
JQueryUI
The following table lists the different options that can be used with this method:
Option
Description
culture
This option sets the culture to use for parsing and formatting the value.
By default its value is null which means the currently set culture in
Globalize is used.
disabled
This option if set to true disables spinner. By default its value is false.
icons
This option sets icons to use for buttons, matching an icon provided by
the jQuery UI CSS Framework. By default its value is { down: "ui-icontriangle-1-s", up: "ui-icon-triangle-1-n" } .
incremental
This option controls the number of steps taken when holding down a spin
button. By default its value is true.
max
This option indicates the maximum allowed value. By default its value
isnull which means there is no maximum enforced.
min
This option indicates the minimum allowed value. By default its value
isnull which means there is no minimum enforced.
This option indicates the number of steps to take when paging via the
pageUp/pageDown methods. By default its value is 10.
step
This option indicates size of the step to take when spinning via buttons
or via the stepUp()/stepDown() methods. The element's step attribute is
used if it exists and the option is not explicitly set.
The following section will show you a few working examples of spinner widget functionality.
Default Functionality
The following example demonstrates a simple example of spinner widget functionality,
passing no parameters to the spinner() method .
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
236
JQueryUI
JQueryUI
238
JQueryUI
In the above example, you can see in the first spinner the max and min values are set to
10 and -10 respectively. Hence crossing these values, the spinner will stop
incrementing/decrementing.
In
the
second
spinner
the
spinner
value
increments/decrements in steps of 100.
JQueryUI
<body>
<!-- HTML -->
<div id="example">
<input type="text" id="spinner-5" value="0" />
</div>
</body>
</html>
Let's save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output. Now, you can
play with the result:
0
In the above example, you can notice the images spinner are changed.
three
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Spinner functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<script src="/jqueryui/jquery-ui1.10.4/external/[Link]"></script>
<script src="/jqueryui/jquery-ui-1.10.4/external/[Link]"></script>
<script src="/jqueryui/jquery-ui-1.10.4/external/[Link]"></script>
<script>
$(function() {
$( "#spinner-4" ).spinner({
culture:"de-DE",
numberFormat:"C",
step:2,
240
JQueryUI
page:10
});
});
</script>
</head>
<body>
<p>
<label for="spinner-4">Amount to donate:</label>
<input id="spinner-4" name="spinner" value="5">
</p>
</body>
</html>
Let's save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output. Now, you can
play with the result:
Amount to donate:
In the above example you can see the spinner displays the number in currency format as
the numberFormat is set to "C" and culture is set to "de-DE". Here we have used the
Globalize files from the jquery-ui library.
Syntax
$(selector, context).spinner ("action", params);;
The following table lists the different actions that can be used with this method:
Action
Description
destroy
disable
JQueryUI
enable
option( optionName )
option
option( optionName, value This action sets the value of the spinner option associated
)
with the specified optionName.
option( options )
pageDown( [pages ] )
pageUp( [pages ] )
stepDown( [steps ] )
stepUp( [steps ] )
value
value( value )
widget
The following examples demonstrate how to use the actions given in the above table.
the
use
of
stepUp,
stepDown,
242
JQueryUI
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Spinner functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<!-- CSS -->
<style type="text/css">
#spinner-6 input {width: 100px}
</style>
<!-- Javascript -->
<script>
$(function() {
$("#spinner-6").spinner();
$('button').button();
$('#stepUp-2').click(function () {
$("#spinner-6").spinner("stepUp");
});
$('#stepDown-2').click(function () {
$("#spinner-6").spinner("stepDown");
});
$('#pageUp-2').click(function () {
$("#spinner-6").spinner("pageUp");
});
$('#pageDown-2').click(function () {
$("#spinner-6").spinner("pageDown");
});
});
</script>
243
JQueryUI
</head>
<body>
<!-- HTML -->
<input id="spinner-6" />
<br/>
<button id="stepUp-2">Increment</button>
<button id="stepDown-2">Decrement</button>
<button id="pageUp-2">Increment Page</button>
<button id="pageDown-2">Decrement Page</button>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output:
Increment
Decrement
Increment Page
Decrement Page
In the above example, use the respective buttons to increment/decrement the spinner.
JQueryUI
$(function() {
$("#spinner-7").spinner();
$('button').button();
$('#stepUp-3').click(function () {
$("#spinner-7").spinner("enable");
});
$('#stepDown-3').click(function () {
$("#spinner-7").spinner("disable");
});
});
</script>
</head>
<body>
<!-- HTML -->
<input id="spinner-7" />
<br/>
<button id="stepUp-3">Enable</button>
<button id="stepDown-3">Disable</button>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output:
Enable
Disable
In the above example, use the Enable/Disable buttons to enable or disable the spinner.
Description
change(event, ui)
create(event, ui)
JQueryUI
spin(event, ui)
start(event, ui)
stop(event, ui)
Example
The following example demonstrates the event method usage in spinner widgets. This
example demonstrates the use of events spin, change and stop.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Spinner functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<!-- CSS -->
<style type="text/css">
#spinner-8 input {width: 100px}
</style>
<!-- Javascript -->
<script>
$(function() {
$( "#spinner-8" ).spinner({
spin: function( event, ui ) {
var result = $( "#result-2" );
[Link]( "Spin Value: "+$( "#spinner-8"
).spinner("value") );
},
change: function( event, ui ) {
var result = $( "#result-2" );
[Link]( "Change value: "+$( "#spinner-8"
).spinner("value") );
},
246
JQueryUI
In the above example change the value of the spinner and see, the messages being
displayed below for spin and stop events. Now change the focus of the spinner and you
see a message being displayed on change event.
Extension Points
The spinner widget is built with the widget factory and can be extended. To extend widgets,
we can either override or add to the behavior of existing methods. Following method
provides as extension point with the same API stability as the spinner methods.
Method
_buttonHtml(event)
Description
247
JQueryUI
_uiSpinnerHtml(event)
The jQuery UI Widget Factory is an extensible base on which all of jQuery UI's widgets are
built. Using the widget factory to build a plugin provides conveniences for state
management, as well as conventions for common tasks like exposing plugin methods and
changing options after instantiation.
248
JQueryUI
Tabs are sets of logically grouped content that allow us to quickly flip between them to.
Tabs allow us to save space like accordions. For tabs to work properly following set of
markups needs to use:
jQueryUI provides us tabs () method drastically changes the appearance of HTML elements
inside the page. This method traverses (internally in jQuery UI) HTML code and adds new
CSS classes to the elements concerned (here, the tabs) to give them the appropriate style.
Syntax
The tabs () method can be used in two forms:
Syntax
$(selector, context).tabs (options);
You can provide one or more options at a time using JavaScript object. If there are more
than one options to be provided then you will separate them using a comma as follows:
Description
This option specifies the current active tab/panel. By default its value is 0.
active
249
JQueryUI
Boolean: When set to false, will collapse all the panels. This requires the
collapsible option to be true.
Integer:
The zero-based index of the panel that is active (open). A negative value
selects panels going backward from the last panel.
Syntax
$( ".selector" ).tabs (
{ active: 1 }
);
This option set to true, it allows tabs to be deselected. When set to false
(the default), clicking on a selected tab does not deselect (it remains
selected). By default its value is false.
Syntax
collapsible
$( ".selector" ).tabs (
{ collapsible: true }
);
This option uses an array to indicate index tabs that are disabled (and
therefore cannot be selected). For example, use [0, 1] to disable the first
two tabs. By default its value is false.
This can be of type:
disabled
Array:
JQueryUI
Syntax
$( ".selector" ).tabs (
{ disabled: [ 0, 2 ] }
);
This option is a name of the event that lets users select a new tab. If, for
example, this option is set to "mouseover", passing the mouse over a tab
will select it. By default its value is "click".
Syntax
event
$( ".selector" ).tabs (
{ event: "mouseover" }
);
This option controls the height of the tabs widget and each panel. By default
its value is "content".
Possible values are:
auto: All panels will be set to the height of the tallest panel.
fill: Expand to the available height based on the tabs' parent height.
heightStyle
content: Each panel will be only as tall as its content.
Syntax
$( ".selector" ).tabs (
{ heightStyle: "fill" }
);
hide
This option specifies how to animate hiding of panel. By default its value
isnull.
251
JQueryUI
Boolean: When set to false, no animation will be used and the panel will be
hidden immediately.
Number:
The panel will fade out with the specified duration and the default easing.
String:
The panel will be hidden using the specified effect. Value can be slideUp or
fold
Object:
For this type, properties effect, delay, duration and easing may be provided.
Syntax
$( ".selector" ).tabs (
{ { hide: { effect: "explode", duration: 1000 } } }
);
This option specifies how to animate showing of panel. By default its value
isnull.
This can be of type:
Boolean: When set to false, no animation will be used and the panel will be
shown immediately.
show
Number:
The panel will fade out with the specified duration and the default easing.
String:
252
JQueryUI
The panel will be shown using the specified effect. Value can be slideUp or
fold.
Object:
For this type, properties effect, delay, duration and easing may be provided.
Syntax
$( ".selector" ).tabs (
{ show: { effect: "blind", duration: 800 } }
);
The following section will show you a few working examples of tabs functionality.
Default Functionality
The following example demonstrates a simple example of tabs functionality, passing no
parameters to the tabs() method.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Tabs functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<script>
$(function() {
$( "#tabs-1" ).tabs();
});
</script>
<style>
#tabs-1{font-size: 14px;}
.ui-widget-header {
background:#b9cd6d;
border: 1px solid #b9cd6d;
253
JQueryUI
color: #FFFFFF;
font-weight: bold;
}
</style>
</head>
<body>
<div id="tabs-1">
<ul>
<li><a href="#tabs-2">Tab 1</a></li>
<li><a href="#tabs-3">Tab 2</a></li>
<li><a href="#tabs-4">Tab 3</a></li>
</ul>
<div id="tabs-2">
<p>Neque porro quisquam est qui dolorem ipsum quia dolor sit
amet, consectetur, adipisci velit... </p>
</div>
<div id="tabs-3">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat. </p>
</div>
<div id="tabs-4">
<p>ed ut perspiciatis unde omnis iste natus error sit
voluptatem accusantium doloremque laudantium, totam rem aperiam,
eaque ipsa quae ab illo inventore veritatis et quasi architecto
beatae vitae dicta sunt explicabo.
</p>
JQueryUI
Let's save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you should see the following output. Now, you can play
with the result:
Tab 1
Tab 2
Tab 3
Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci
velit...
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
ed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque
laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi
architecto beatae vitae dicta sunt explicabo.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat.
In the above example, click on tabs to swap between content.
JQueryUI
});
</script>
<style>
#tabs-5{font-size: 14px;}
.ui-widget-header {
background:#b9cd6d;
border: 1px solid #b9cd6d;
color: #FFFFFF;
font-weight: bold;
}
</style>
</head>
<body>
<div id="tabs-5">
<ul>
<li><a href="#tabs-6">Tab 1</a></li>
<li><a href="#tabs-7">Tab 2</a></li>
<li><a href="#tabs-8">Tab 3</a></li>
</ul>
<div id="tabs-6">
<p>Neque porro quisquam est qui dolorem ipsum quia dolor
sit amet, consectetur, adipisci velit... </p>
</div>
<div id="tabs-7">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat. </p>
</div>
<div id="tabs-8">
<p>ed ut perspiciatis unde omnis iste natus error sit voluptatem
accusantium doloremque laudantium, totam rem aperiam, eaque ipsa
quae
ab illo inventore veritatis et quasi architecto beatae vitae dicta
sunt explicabo.
</p>
JQueryUI
Tab 1
Tab 2
Tab 3
Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci
velit...
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
ed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque
laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi
architecto beatae vitae dicta sunt explicabo.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
Click the selected tab to toggle its content closed/open. You can also see the animation
effect "slideUp" when the tab is closed.
Use of Event
The following example demonstrates the usage of three options event in the tabs function
of JqueryUI.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Tabs functionality</title>
<link href="[Link] rel="stylesheet">
257
JQueryUI
<script src="[Link]
<script src="[Link]
<script>
$(function() {
$( "#tabs-9" ).tabs({
event:"mouseover"
});
});
</script>
<style>
#tabs-9{font-size: 14px;}
.ui-widget-header {
background:#b9cd6d;
border: 1px solid #b9cd6d;
color: #FFFFFF;
font-weight: bold;
}
</style>
</head>
<body>
<div id="tabs-9">
<ul>
<li><a href="#tabs-10">Tab 1</a></li>
<li><a href="#tabs-11">Tab 2</a></li>
<li><a href="#tabs-12">Tab 3</a></li>
</ul>
<div id="tabs-10">
<p>Neque porro quisquam est qui dolorem ipsum quia dolor
sit amet, consectetur, adipisci velit... </p>
</div>
<div id="tabs-11">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat. </p>
</div>
258
JQueryUI
<div id="tabs-12">
<p>ed ut perspiciatis unde omnis iste natus error sit
voluptatem accusantium doloremque laudantium, totam rem aperiam,
eaque ipsa quae ab illo inventore veritatis et quasi architecto
beatae vitae dicta sunt explicabo.
</p>
Tab 1
Tab 2
Tab 3
Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci
velit...
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
ed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque
laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi
architecto beatae vitae dicta sunt explicabo.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
In the above example toggle the sections open/closed with mouseover the tabs.
259
JQueryUI
Syntax
$(selector, context).tabs ("action", params);;
Following table lists the different actions that can be used with this method:
Action
destroy
Description
This action destroys the tabs functionality of an element
completely. The elements return to their pre-init state. This
method does not accept any arguments.
Syntax
$( ".selector" ).tabs("destroy");
This action disables all tabs. This method does not accept any
arguments.
disable
Syntax
$( ".selector" ).tabs("disable");
This action disables the specified tab. Where index is the tab
to be disabled.
disable( index )
Syntax
$( ".selector" ).tabs( "disable", 1 );
This action activates all the tabs. This signature does not
accept any arguments.
enable
Syntax
$( ".selector" ).tabs("enable");
This action activates a specified tab. Where index is the tab
to be enabled.
enable( index )
Syntax
$( ".selector" ).tabs( "enable", 1 );
This action forces a reload of the indexed tab, ignoring the
cache. Where index is the tab to load.
load( index )
Syntax
$( ".selector" ).tabs("load", 1);
option( optionName )
260
JQueryUI
Syntax
Syntax
$( ".selector" ).tabs("option");
option(optionName,
value)
This action sets the value of the tabs option associated with
the specified optionName. The argument optionName is
name of the option to be set and value is the value to be set
for the option.
Syntax
var isDisabled = $( ".selector" ).tabs( "option", "disabled" );
This action sets one or more options to the tabs.
option( options )
Syntax
$( ".selector" ).tabs( "option", { disabled: true } );
This action recomputes the height of the tab panels when any
tabs that were added or removed directly in the DOM. Results
depend on the content and the heightStyle option.
refresh
Syntax
Syntax
261
JQueryUI
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Tabs functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<script>
$(function() {
$( "#tabs-13" ).tabs();
$( "#tabs-13" ).tabs("disable");
});
</script>
<style>
#tabs-13{font-size: 14px;}
.ui-widget-header {
background:#b9cd6d;
border: 1px solid #b9cd6d;
color: #FFFFFF;
font-weight: bold;
}
</style>
</head>
<body>
<div id="tabs-13">
<ul>
<li><a href="#tabs-14">Tab 1</a></li>
<li><a href="#tabs-15">Tab 2</a></li>
262
JQueryUI
</p>
Tab 1
Tab 2
Tab 3
263
JQueryUI
Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci
velit...
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
ed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque
laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi
architecto beatae vitae dicta sunt explicabo.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
Here you can see all the tabs are disabled.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Tabs functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<script>
$(function() {
$( "#tabs-17" ).tabs();
$( "#tabs-17" ).tabs("disable",2);
});
</script>
<style>
#tabs-17{font-size: 14px;}
.ui-widget-header {
background:#b9cd6d;
border: 1px solid #b9cd6d;
264
JQueryUI
color: #FFFFFF;
font-weight: bold;
}
</style>
</head>
<body>
<div id="tabs-17">
<ul>
<li><a href="#tabs-18">Tab 1</a></li>
<li><a href="#tabs-19">Tab 2</a></li>
<li><a href="#tabs-20">Tab 3</a></li>
</ul>
<div id="tabs-18">
<p>Neque porro quisquam est qui dolorem ipsum quia dolor
sit amet, consectetur, adipisci velit... </p>
</div>
<div id="tabs-19">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco
laboris
nisi ut aliquip ex ea commodo consequat. </p>
</div>
<div id="tabs-20">
<p>ed ut perspiciatis unde omnis iste natus error sit
voluptatem
accusantium doloremque laudantium, totam rem aperiam, eaque
ipsa quae
ab illo inventore veritatis et quasi architecto beatae vitae
dicta
sunt explicabo.
</p>
JQueryUI
Tab 1
Tab 2
Tab 3
Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci
velit...
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
ed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque
laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi
architecto beatae vitae dicta sunt explicabo.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
In the above example, you notice that Tab 3 is disabled.
Description
This event is triggered after the tab has been activated (after
the completion of animation).
activate(event, ui)
JQueryUI
Syntax
$( ".selector" ).slider({
activate: function( event, ui ) {}
});
beforeActivate(event,
ui)
Syntax
$( ".selector" ).slider({
beforeActivate: function( event, ui ) {}
});
267
JQueryUI
beforeLoad(event, ui)
Syntax
$( ".selector" ).slider({
beforeLoad: function( event, ui ) {}
});
Syntax
$( ".selector" ).slider({
create: function( event, ui ) {}
268
JQueryUI
});
load(event, ui)
Syntax
$( ".selector" ).slider({
load: function( event, ui ) {}
});
Example
The following example demonstrates the event method usage in tabs widgets. This
example demonstrates the use of events activate and create.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Tabs functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<script>
$(function() {
$( "#tabs-21" ).tabs({
activate: function( event, ui ) {
269
JQueryUI
JQueryUI
</ul>
<div id="tabs-22">
<p>Neque porro quisquam est qui dolorem ipsum quia dolor
sit amet, consectetur, adipisci velit... </p>
</div>
<div id="tabs-23">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco
laboris
nisi ut aliquip ex ea commodo consequat. </p>
</div>
<div id="tabs-24">
<p>ed ut perspiciatis unde omnis iste natus error sit
voluptatem
accusantium doloremque laudantium, totam rem aperiam, eaque
ipsa quae
ab illo inventore veritatis et quasi architecto beatae vitae
dicta
sunt explicabo.
</p>
JQueryUI
Tab 1
Tab 2
Tab 3
Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci
velit...
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
ed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque
laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi
architecto beatae vitae dicta sunt explicabo.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
Click on the tabs to see a message getting printed below on specific to events.
272
JQueryUI
Tooltip widget of jQueryUI replaces the native tooltips. This widget adds new themes and
allows for customization. First let us understand what tooltips are? Tooltips can be attached
to any element. To display tooltips, just add title attribute to input elements and title
attribute value will be used as tooltip. When you hover the element with your mouse, the
title attribute is displayed in a little box next to the element.
jQueryUI provides tooltip() method to add tooltip to any element on which you want to
display tooltip. This gives a fade animation by default to show and hide the tooltip,
compared to just toggling the visibility.
Syntax
The tooltip() method can be used in two forms:
Syntax
$(selector, context).tooltip(options);
You can provide one or more options at a time using Javascript object. If there are more
than one options to be provided then you will separate them using a comma as follows:
$(selector, context).tooltip({option1: value1, option2: value2..... });
The following table lists the different options that can be used with this method:
Option
Description
This option represents content of a tooltip. By default its value is function
returning the title attribute.
This can be of type:
content
Function: The callback can either return the content directly, or call the first
argument, passing in the content, eg. for ajax content.
273
JQueryUI
Syntax
$(".selector").tooltip(
{ content: "Some content!" }
);
This option when set to true disables the tooltip. By default its value is false.
Syntax
disabled
$(".selector").tooltip(
{ disabled: true }
);
This option represents the animation effect when hiding the tooltip. By
default its value is true.
This can be of type:
Boolean: This can be true or false. When set to true, the tooltip will fade out
with the default duration and the default easing.
Number: The tooltip will fade out with the specified duration and the default
easing.
hide
String: The tooltip will be hidden using the specified effect such as "slideUp",
"fold".
Syntax
$(".selector").tooltip(
274
JQueryUI
$(".selector").tooltip(
{ items: "img[alt]" }
);
This option decides the position of the tooltip w.r.t the associated target
element. By default its value is function returning the title attribute. Possible
values are: my, at, of, collision, using, within.
Syntax
position
$(".selector").tooltip(
{ { my: "left top+15", at: "left bottom", collision: "flipfit" } }
);
This option represents how to animate the showing of tooltip. By default its
value is true.
This can be of type:
Boolean: This can be true or false. When set to true, the tooltip will fade out
with the default duration and the default easing.
show
Number: The tooltip will fade out with the specified duration and the default
easing.
String: The tooltip will be hidden using the specified effect such as "slideUp",
"fold".
Syntax
275
JQueryUI
$(".selector").tooltip(
{ show: { effect: "blind", duration: 800 } }
);
This option is a class which can be added to the tooltip widget for tooltips
such as warning or errors. By default its value is null.
Syntax
tooltipClass
$(".selector").tooltip(
{ tooltipClass: "custom-tooltip-styling" } }
);
This option when set to true, the tooltip follows/tracks the mouse. By default
its value is false.
Syntax
track
$(".selector").tooltip(
{ track: true }
);
The following section will show you a few working examples of tooltip functionality.
Default Functionality
The following example demonstrates a simple example of tooltip functionality passing no
parameters to the tooltip() method .
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Tooltip functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
276
JQueryUI
<script>
$(function() {
$("#tooltip-1").tooltip();
$("#tooltip-2").tooltip();
});
</script>
</head>
<body>
<!-- HTML -->
<label for="name">Name:</label>
<input id="tooltip-1" title="Enter You name">
<p><a id="tooltip-2" href="#" title="Nice tooltip">
I also have a tooltip
</a></p>
</body>
</html>
Let's save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output. Now, you can
play with the result:
Name:
I also have a tooltip
In the above example, hover over the links above or use the tab key to cycle the focus on
each element.
JQueryUI
<script src="[Link]
Message:
Tooltip disabled for me:
In the above example, the content of tooltip of first box is set using content option. You
can also notice the tooltip follows the mouse. The tooltip for second input box is disabled.
278
JQueryUI
Use of Position
The following example shows the usage of option position in the tooltip function of
JqueryUI.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Tooltip functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<!-- CSS -->
<style>
body {
margin-top: 100px;
}
.ui-tooltip-content::after, .ui-tooltip-content::before {
content: "";
position: absolute;
border-style: solid;
display: block;
left: 90px;
}
.ui-tooltip-content::before {
bottom: -10px;
border-color: #AAA transparent;
border-width: 10px 10px 0;
}
.ui-tooltip-content::after {
bottom: -7px;
border-color: white transparent;
border-width: 10px 10px 0;
}
</style>
279
JQueryUI
Syntax
$(selector, context).tooltip ("action", [params]);
280
JQueryUI
The following table lists the actions for this method:
Action
Description
This action closes the tooltip. This method does not accept
any arguments.
close()
Syntax
$(".selector").tooltip("close");
destroy()
disable()
Syntax
$(".selector").tooltip("disable");
This action activates the tooltip. This method does not
accept any arguments.
enable()
Syntax
$(".selector").tooltip("enable");
This action programmatically opens the tooltip. This method
does not accept any arguments.
open()
Syntax
$(".selector").tooltip("open");
This action gets the value associated with optionName for
the tooltip. This method does not accept any arguments.
option( optionName )
Syntax
var isDisabled = $( ".selector" ).tooltip( "option", "disabled"
);
option()
JQueryUI
Syntax
$( ".selector" ).tooltip( "option", { disabled: true } );
This action returns a jQuery object containing the original
element. This method does not accept any arguments.
widget()
Syntax
$(".selector").tooltip("widget");
Examples
Now let us see an example using the actions from the above table. The following example
demonstrates the use of actions disable and enable.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Tooltip functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<!-- Javascript -->
<script>
$(function() {
$("#tooltip-8").tooltip({
//use 'of' to link the tooltip to your specified input
position: { of: '#myInput', my: 'left center', at: 'left center'
},
}),
$('#myBtn').click(function () {
282
JQueryUI
$('#tooltip-8').tooltip("open");
});
});
</script>
</head>
<body style="padding:100px;">
<!-- HTML -->
<a id="tooltip-8" title="Message" href="#"></a>
<input id="myInput" type="text" name="myInput" value="0" size="7" />
<input id="myBtn" type="submit" name="myBtn" value="myBtn" class="myBtn"
/>
</body>
</html>
Let's save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output:
0
myBtn
In the above example, click on myBtn button and a tooltip pops up.
Description
Triggered when the tooltip is created. Where event is of type Event,
andui is of type Object.
create(event,
ui)
Syntax
$(".selector").tooltip(
create: function(event, ui) {}
);
close(event, ui)
Triggered
when
the
tooltip
is
closed.
Usually
triggers
on focusout ormouseleave. Where event is of type Event, and ui is of
type Object.
Possible values of ui are:
tooltip: A generated tooltip element.
283
JQueryUI
Syntax
$(".selector").tooltip(
close: function(event, ui) {}
);
Triggered when the tooltip is displayed or shown. Usually triggered
onfocusin or mouseover. Where event is of type Event, and ui is of
typeObject.
Possible values of ui are:
open(event, ui)
Examples
The following example demonstrates the use of event method during tooltip functionality.
This example demonstrates use of open and close events.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Tooltip functionality</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<!-- Javascript -->
<script>
$(function() {
$('.tooltip-9').tooltip({
items: '[Link]-9',
content: 'Hello welcome',
show: "slideDown", // show immediately
open: function(event, ui)
284
JQueryUI
{
[Link](
function () {
$(this).fadeTo("slow", 0.5);
});
}
});
});
$(function() {
$('.tooltip-10').tooltip({
items: '[Link]-10',
content: 'Welcome to TutorialsPoint',
show: "fold",
close: function(event, ui)
{
[Link](function()
{
$(this).stop(true).fadeTo(500, 1);
},
function()
{
$(this).fadeOut('500', function()
{
$(this).remove();
});
});
}
});
});
</script>
</head>
<body style="padding:100px;">
<!-- HTML -->
<div id="target">
<a href="#" class="tooltip-9">Hover over me!</a>
<a href="#" class="tooltip-10">Hover over me too!</a>
285
JQueryUI
</div>
</body>
</html>
Let's save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output:
In the above example, the tooltip for Hover over me! disappear immediately whereas the
tooltip for Hover over me too! fades out after duration of 1000ms.
286
JQueryUI
287
JQueryUI
This chapter will discuss the addClass() method, which is one of the methods used to
manage jQueryUI visual effects. addClass() method allow animating the changes to the
CSS properties.
addClass() method add specified classes to the matched elements while animating all style
changes.
Syntax
Added In Version 1.0 of jQueryUI
The addClass() method has its basic syntax as follows:
.addClass( className [, duration ] [, easing ] [, complete ] )
Parameter
Description
className
duration
easing
This is of type String and indicates the way to progress in the effect. Its
default value is swing. Possible values are here.
complete
This is a callback method called for each element when the effect is
complete for this element.
288
JQueryUI
Parameter
Description
className
options
This represents all animation settings. All properties are optional. Possible
values are:
duration: This is of type Number or String, and indicates the number of
milliseconds of the effect. A value of 0 takes the element directly in the
new style, without progress. Its default value is 400.
easing: This is of type String and indicates the way to progress in the
effect. Its default value is swing.
complete: This is a callback method called for each element when the
effect is complete for this element.
children: This is of type Boolean and represents whether the animation
should additionally be applied to all descendants of the matched elements.
Its default value is false.
queue: This is of type Boolean or String and represents whether to place
the animation in the effects queue. Its default value istrue.
Examples
The following example demonstrates the use of addClass() methods.
JQueryUI
.myClass {
font-size: 40px; background-color: #ccc; color: white;
}
</style>
<script type="text/javascript">
$(document).ready(function() {
$('.button').click(function() {
if ([Link] == "add") {
$('#animTarget').addClass("myClass", "fast")
} else {
$('#animTarget').removeClass("myClass", "fast")
}
})
});
</script>
</head>
<body>
<div id=animTarget class="elemClass">
Hello!
</div>
<button class="button" id="add">Add Class</button>
<button class="button" id="remove">Remove Class</button>
</body>
</html>
Let's save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output. Now, you can
play with the result:
Hello!
Add Class Remove Class
Click on the Add Class and Remove Class buttons to see the effect of classes on the box.
JQueryUI
<head>
<meta charset="utf-8">
<title>jQuery UI addClass Example</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<!-- CSS -->
<style>
.red { color: red; }
.big { font-size: 5em; }
.spaced { padding: 1em; }
</style>
<script>
$(document).ready(function() {
$('.button-1').click(function() {
$( "#welcome" ).addClass( "red big spaced", 3000 );
});
});
</script>
</head>
<body>
<p id="welcome">Welcome to Tutorials Point!</p>
<button class="button-1">Click me</button>
</body>
</html>
Let's save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output. Now, you can
play with the result:
Tutorials Point!
Click Me
291
JQueryUI
jQueryUI extends some core jQuery methods so that you can animate different transitions
for an element. One of them being animate method. jQueryUI extends the
jQuery animatemethod, to add support for animating colors. You can animate one of
several CSS properties that define an elements colors. Following are the CSS properties
that theanimate method supports.
Syntax
Following is the syntax of the jQueryUI animate method:
$( "#selector" ).animate(
{ backgroundColor: "black",
color: "white"
}
);
You can set any number of properties in this method separated by , (comma).
Example
The following example demonstrates the use of addClass() methods.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery UI addClass Example</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<style>
292
JQueryUI
.elemClass {
width: 200px;
height: 50px;
background-color: #b9cd6d;
}
.myClass {
font-size: 40px; background-color: #ccc; color: white;
}
</style>
<script type="text/javascript">
$(document).ready(function() {
$('#button-1').click(function() {
$('#animTarget').animate({
backgroundColor: "black",
color: "white"
})
})
});
</script>
</head>
<body>
<div id=animTarget class="elemClass">
Hello!
</div>
<button id="button-1">Click Me</button>
</body>
</html>
Let's save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output. Now, you can
play with the result:
Hello!
Click Me
Click on the button and see how the animation changes of the box.
293
JQueryUI
This chapter will discuss the effect() method, which is one of the methods used to manage
jQueryUI visual effects. effect() method applies an animation effect to the elements
without having to show or hide it.
Syntax
The effect() method has the following syntax:
.effect( effect [, options ] [, duration ] [, complete ] )
Parameter
Description
effect
options
duration
complete
This is a callback method called for each element when the effect is
complete for this element.
jQueryUI Effects
The following table describes the various effects that can be used with the effects()
method:
Effect
blind
Description
Shows or hides the element in the manner of a window blind: by moving the
bottom edge down or up, or the right edge to the right or left, depending
upon the specified direction and mode.
294
JQueryUI
bounce
clip
drop
Shows or hides the element by making it appear to drop onto, or drop off of,
the page.
explode
Shows or hides the element by splitting it into multiple pieces that move in
radial directions as if imploding into, or exploding from, the page.
fade
Shows or hides the element by adjusting its opacity. This is the same as the
core fade effects, but without options.
fold
Shows or hides the element by adjusting opposite borders in or out, and then
doing the same for the other set of borders.
highlight
puff
pulsate
Adjusts the opacity of the element on and off before ensuring that the
element is shown or hidden as specified.
scale
shake
size
Resizes the element to a specified width and height. Similar to scale except
for how the target size is specified.
slide
Moves the element such that it appears to slide onto or off of the page.
295
JQueryUI
transfer
Examples
The following examples demonstrates the use of effect() method with different effect listed
in the above table.
Effect Shake
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI effect Example</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<!-- CSS -->
<style>
#box-1 {
height: 100px;
width: 100px;
background: #b9cd6d;
}
</style>
<script>
$(document).ready(function() {
$('#box-1').click(function() {
$( "#box-1" ).effect( "shake", {
times: 10,
distance: 100
}, 3000, function() {
$( this ).css( "background", "#cccccc" );
});
});
296
JQueryUI
});
</script>
</head>
<body>
<div id="box-1">Click On Me</div>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output. Now, you can
play with the result:
Click on Me
Effect explode
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI effect Example</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<!-- CSS -->
<style>
#box-2 {
height: 100px;
width: 100px;
background: #b9cd6d;
}
</style>
<script>
$(document).ready(function() {
$('#box-2').click(function() {
$( "#box-2" ).effect({
effect: "explode",
easing: "easeInExpo",
297
JQueryUI
pieces: 4,
duration: 5000
});
});
});
</script>
</head>
<body>
<div id="box-2"></div>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output. Now, you can
play with the result:
Click on Me
298
JQueryUI
This chapter will discuss the hide() method, which is one of the methods used to manage
jQueryUI visual effects. effect() method applies an animation effect to hide elements.
Syntax
The hide() method has the following syntax:
.hide( effect [, options ] [, duration ] [, complete ] )
Parameter
Description
effect
options
duration
complete
This is a callback method called for each element when the effect is
complete for this element.
jQueryUI Effects
The following table describes the various effects that can be used with the hide() method:
Effect
Description
blind
Shows or hides the element in the manner of a window blind: by moving the
bottom edge down or up, or the right edge to the right or left, depending
upon the specified direction and mode.
bounce
299
JQueryUI
clip
drop
Shows or hides the element by making it appear to drop onto, or drop off of,
the page.
explode
Shows or hides the element by splitting it into multiple pieces that move in
radial directions as if imploding into, or exploding from, the page.
fade
Shows or hides the element by adjusting its opacity. This is the same as the
core fade effects, but without options.
fold
Shows or hides the element by adjusting opposite borders in or out, and then
doing the same for the other set of borders.
highlight
puff
pulsate
Adjusts the opacity of the element on and off before ensuring that the
element is shown or hidden as specified.
scale
shake
size
Resizes the element to a specified width and height. Similar to scale except
for how the target size is specified.
slide
Moves the element such that it appears to slide onto or off of the page.
transfer
300
JQueryUI
Examples
The following examples demonstrates the use of hide() method with different effect listed
in the above table.
Effect - Blind
The following example shows the use of hide() method with blind effect. Click on the
buttonBlind Effect Hide to see the blind effect before the element hides.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI hide Example</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<!-- CSS -->
<style>
.toggler { width: 500px; height: 200px; }
#button { padding: .5em 1em; text-decoration: none; }
#effect { width: 240px; height: 135px; padding: 0.4em; position:
relative; }
#effect h3 { margin: 0; padding: 0.4em; text-align: center; }
</style>
<script>
$(function() {
function runEffect() {
$( "#effect" ).hide( "blind", {times: 10, distance: 100}, 1000,
callback );
};
// callback function to bring a hidden box back
function callback() {
setTimeout(function() {
$( "#effect" ).removeAttr( "style" ).hide().fadeIn();
}, 1000 );
};
301
JQueryUI
$( "#button" ).click(function() {
runEffect();
return false;
});
});
</script>
</head>
<body>
<div class="toggler">
<div id="effect" class="ui-widget-content ui-corner-all">
<h3 class="ui-widget-header ui-corner-all">Hide</h3>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore.
</p>
</div>
</div>
Hide
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore.
Effect - Shake
The following example shows the use of shake() method with blind effect. Click on the
button Shake Effect Hide to see the shake effect before the element hides.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
302
JQueryUI
JQueryUI
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore.
</p>
</div>
</div>
<a href="#" id="button-1" class="ui-state-default ui-corner-all">Shake
Effect Hide</a>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output. Now, you can
play with the result:
Hide
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore.
304
JQueryUI
This chapter will discuss the removeClass() method, wh ich is one of the methods used
to manage jQueryUI visual effects. removeClass() method removes the applied classes
from the elements.
removeClass() method removes the specified classes to the matched elements while
animating all style changes.
Syntax
Added In Version 1.0 of jQueryUI
The removeClass() method has its basic syntax as follows:
.removeClass(
Parameter
Description
className
duration
easing
This is of type String and indicates the way to progress in the effect. Its
default value is swing. Possible values are here.
complete
This is a callback method called for each element when the effect is complete
for this element.
Parameter
Description
className
options
This represents all animation settings. All properties are optional. Possible
values are:
305
JQueryUI
Examples
The following example demonstrates the use of removeClass() methods.
JQueryUI
$(document).ready(function() {
$('.button').click(function() {
if ([Link] == "add") {
$('#animTarget').addClass("myClass", "fast")
} else {
$('#animTarget').removeClass("myClass", "fast")
}
})
});
</script>
</head>
<body>
<div id=animTarget class="elemClass">
Hello!
</div>
<button class="button" id="add">Add Class</button>
<button class="button" id="remove">Remove Class</button>
</body>
</html>
Let's save the above code in an HTML file [Link] and open it in a
standard browser which supports javascript, you must also see the following output. Now,
you can play with the result:
Hello!
Add Class Remove Class
Click on the Add Class and Remove Class buttons to see the effect of classes on the box.
307
JQueryUI
This chapter will discuss the show() method, which is one of the methods used to manage
jQueryUI visual effects. show() method displays an item using the indicated effect.
show() method toggles the visibility of the wrapped elements using the specified effect.
Syntax
The show() method has the following syntax:
.show( effect [, options ] [, duration ] [, complete ] )
Parameter
Description
effect
options
duration
This is of type Number or String and determines how long the animation will
run. Its default value is 400.
complete
This is a callback method called for each element when the effect is complete
for this element.
jQueryUI Effects
The following table describes the various effects that can be used with the effects()
method:
Effect
Description
blind
Shows or hides the element in the manner of a window blind: by moving the
bottom edge down or up, or the right edge to the right or left, depending upon
the specified direction and mode.
bounce
308
JQueryUI
clip
drop
Shows or hides the element by making it appear to drop onto, or drop off of,
the page.
explode
Shows or hides the element by splitting it into multiple pieces that move in
radial directions as if imploding into, or exploding from, the page.
fade
Shows or hides the element by adjusting its opacity. This is the same as the
core fade effects, but without options.
fold
Shows or hides the element by adjusting opposite borders in or out, and then
doing the same for the other set of borders.
highlight
puff
pulsate
Adjusts the opacity of the element on and off before ensuring that the element
is shown or hidden as specified.
scale
shake
size
Resizes the element to a specified width and height. Similar to scale except for
how the target size is specified.
slide
Moves the element such that it appears to slide onto or off of the page.
transfer
309
JQueryUI
Examples
The following example demonstrates the use of show() method.
JQueryUI
runEffect();
return false;
});
$( "#effect" ).hide();
});
</script>
</head>
<body>
<div class="toggler">
<div id="effect" class="ui-widget-content ui-corner-all">
<h3 class="ui-widget-header ui-corner-all">Show</h3>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore.
</p>
</div>
</div>
<a href="#" id="button" class="ui-state-default ui-corner-all">Run
Effect</a>
</body>
</html>
Let's save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output. Now, you can
play with the result:
Show
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore.
Run Effect
Click on the Add Class and Remove Class buttons to see the effect of classes on the box.
JQueryUI
JQueryUI
<div class="toggler">
<div id="effect" class="ui-widget-content ui-corner-all">
<h3 class="ui-widget-header ui-corner-all">Show</h3>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore.
</p>
</div>
</div>
<a href="#" id="button" class="ui-state-default ui-corner-all">Run
Effect</a>
</body>
</html>
Let's save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output. Now, you can
play with the result:
Show
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore.
Run Effect
313
JQueryUI
This chapter will discuss the switchClass() method, which is a useful new class for
manipulation. switchClass() method move from one CSS one CSS class to another,
animating the transition from one state to the other.
Syntax
Added In Version 1.0 of jQueryUI
The switchClass() method has its basic syntax as follows:
.switchClass( removeClassName, addClassName [, duration ] [, easing ] [, complete ] )
Parameter
Description
removeClassName
This is a String and represents the CSS class name, or spacedelimited list of class names, to be removed.
addClassName
This is of type String and represents one or more class names (space
separated) to be added to the class attribute of each matched
element.
duration
easing
complete
This is a callback method called for each element when the effect is
complete for this element.
Parameter
Description
removeClassName
This is a String and represents the CSS class name, or spacedelimited list of class names, to be removed.
314
JQueryUI
addClassName
This is of type String and represents one or more class names (space
separated) to be added to the class attribute of each matched
element.
This represents all animation settings. All properties are optional.
Possible values are:
duration: A string or number determining how long the animation will
run.. Its default value is 400.
easing: A string indicating which easing function to use for the
transition. Its default value is swing. Possible values are here.
options
complete: This is a callback method called for each element when the
effect is complete for this element.
children: This is a Boolean and represents whether the animation
should additionally be applied to all descendants of the matched
elements.
queue: This is of type String/Boolean indicating whether to place the
animation in the effects queue..
Examples
The following example demonstrates the use of switchClass() method.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Switch Class Example</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<!-- CSS -->
<style>
.LargeClass
{
font-family: Arial;
font-size: large;
font-weight: bold;
color: Red;
315
JQueryUI
}
.NormalClass
{
font-family: Arial;
font-size: small;
font-weight: bold;
color: Blue;
}
</style>
<script>
$(function() {
$('#btnSwitch').click(function(){
$(".NormalClass").switchClass("NormalClass","LargeClass",'fast');
$(".LargeClass").switchClass("LargeClass","NormalClass",'fast');
return false;
});
});
</script>
</head>
<body>
<div class="NormalClass">
Tutorials Point Rocks!!!
</div>
<div class="NormalClass">
Welcome to Tutorials Point!!!
</div>
<br />
<input type="button" id="btnSwitch" value="Switch Class" />
</body>
</html>
Let's save the above code in an HTML file [Link] and open it in a
standard browser which supports javascript, you must also see the following output. Now,
you can play with the result:
Tutorials Point Rocks!!!
Welcome to Tutorials Point!!!
316
JQueryUI
Switch Class
Click on the Switch Class button to see the effect of classes on the box.
317
JQueryUI
This chapter will discuss the toggle() method of jQueryUI visual effects. toggle() method
toggles the show () or hide () methods depending on whether the element is hidden or
not.
Syntax
The toggle() method has the following syntax:
.toggle( effect [, options ] [, duration ] [, complete ] )
Parameter
Description
effect
options
duration
This is of type Number or String and determines how long the animation will
run. Its default value is 400.
complete
This is a callback method called for each element when the effect is complete
for this element.
jQueryUI Effects
The following table describes the various effects that can be used with the effects()
method:
Effect
Description
blind
Shows or hides the element in the manner of a window blind: by moving the
bottom edge down or up, or the right edge to the right or left, depending upon
the specified direction and mode.
bounce
318
JQueryUI
clip
drop
Shows or hides the element by making it appear to drop onto, or drop off of,
the page.
explode
Shows or hides the element by splitting it into multiple pieces that move in
radial directions as if imploding into, or exploding from, the page.
fade
Shows or hides the element by adjusting its opacity. This is the same as the
core fade effects, but without options.
fold
Shows or hides the element by adjusting opposite borders in or out, and then
doing the same for the other set of borders.
highlight
puff
pulsate
Adjusts the opacity of the element on and off before ensuring that the element
is shown or hidden as specified.
scale
shake
size
Resizes the element to a specified width and height. Similar to scale except for
how the target size is specified.
slide
Moves the element such that it appears to slide onto or off of the page.
transfer
319
JQueryUI
Example
The following example demonstrates the use of toggle() method with different effect listed
in the above table.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Toggle Example</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<!-- CSS -->
<style>
.toggler { width: 500px; height: 200px; }
#button { padding: .5em 1em; text-decoration: none; }
#effect { width: 240px; height: 135px; padding: 0.4em; position:
relative; }
#effect h3 { margin: 0; padding: 0.4em; text-align: center; }
</style>
<script>
$(function() {
function runEffect() {
$( "#effect" ).toggle('explode', 300);
};
$( "#button" ).click(function() {
runEffect();
return false;
});
});
</script>
</head>
<body>
<div class="toggler">
<div id="effect" class="ui-widget-content ui-corner-all">
<h3 class="ui-widget-header ui-corner-all">Toggle</h3>
320
JQueryUI
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore.
</p>
</div>
</div>
<a href="#" id="button" class="ui-state-default ui-corner-all">Toggle</a>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a standard
browser which supports javascript, you must also see the following output. Now, you can
play with the result:
Toggle
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore.
Click on the Toggle button to check how the classes are shown and hidden.
321
JQueryUI
322
JQueryUI
This chapter will discuss the toggleClass() method, which is a useful new class for
manipulation. toggleClass() method adds or removes one or more classes from each
element in the set of matched elements.
Syntax
Added In Version 1.0 of jQueryUI
The toggleClass() method has its basic syntax as follows:
.toggleClass( className [, switch ] [, duration ] [, easing ] [, complete ] )
Parameter Description
className
This is a String and represents the CSS class name, or space-delimited list
of class names, to be added, removed, or toggled.
switch
duration
This is of type Number or String and optionally provides one of slow, normal,
fast, or the duration of the effect in milliseconds. If omitted, the animate()
method determines the default. Its default value is 400.
easing
complete
This is a callback method called for each element when the effect is complete
for this element.
Description
className
This is a String and represents the CSS class name, or space-delimited list
of class names, to be added, removed, or toggled.
switch
JQueryUI
This represents all animation settings. All properties are optional. Possible
values are:
duration: A string or number determining how long the animation will run..
Its default value is 400.
easing: A string indicating which easing function to use for the transition.
Its default value is swing. Possible values are here.
options
complete: This is a callback method called for each element when the effect
is complete for this element.
children: This is a Boolean and represents whether the animation should
additionally be applied to all descendants of the matched elements.
queue: This is of type String/Boolean indicating whether to place the
animation in the effects queue.
Examples
The following example demonstrates the use of toggleClass() method.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Switch Class Example</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<!-- CSS -->
<style>
.class1 {
border-width : 10px;
border-color : grey;
background-color : #cedc98;
color : black;
}
</style>
<script>
function toggle ()
{
324
JQueryUI
325
JQueryUI
326
JQueryUI
In this chapter we shall see one of the utility methods of jqueryUi, the position() method.
The position() method allows you to position an element with respect to another element
or mouse event.
jQuery UI extends the .position() method from jQuery core in a way that lets you describe
how you want to position an element the same way you would naturally describe it to
another person. Instead of working with numbers and math, you work with meaningful
words (such as left and right) and relationships.
Syntax
Following is the syntax of the position() method:
.position( options )
Where options is of type Object and provides the information that specifies how the
elements of the wrapped set are to be positioned. Following table lists the
different optionsthat can be used with this method:
Option
Description
This option specifies the location of the wrapped elements (the ones being repositioned) to align with the target element or location. By default its value is
center.
my
Two of these values are used to specify location: top, left, bottom, right, and
center, separated by a space character, where the first value is the horizontal
value, and the second the vertical. Whether the specified single value is
considered horizontal or vertical depends upon which value you use (for
example, top is taken as vertical, while right is horizontal).
Example
top, or bottom right.
This option is of type String and specifies the location of the target element
against which to align the re-positioned elements. Takes the same values as
themy option. By default its value is center.
at
Example
327
JQueryUI
flip: Flips the element to the opposing side and runs collision detection again
for fit. If neither side fits, center is used as a fallback.
fit: Keeps the element in the desired direction, but adjusts the position such
that it will fit.
collision flipfit: First applies the flip logic, placing the element on whichever side allows
more of the element to be visible. Then the fit logic is applied to ensure as much
of the element is visible as possible.
Example
using
This option is a function that replaces the internal function that changes the
element position. Called for each wrapped element with a single argument that
consists of an object hash with the left and top properties set to the computed
target position, and the element set as the function context. By default its value
is null.
Example
{horizontal: "center", vertical: "left", important: "horizontal" }
within
This option is a Selector or Element or jQuery element, and allows you to specify
which element to use as the bounding box for collision detection. This can come
328
JQueryUI
in handy if you need to contain the positioned element within a specific section
of your page. By default its value is window.
Example
The following example demontstrates the use of position method.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI position method Example</title>
<link href="[Link] rel="stylesheet">
<script src="[Link]
<script src="[Link]
<!-- CSS -->
<style>
.positionDiv {
position: absolute;
width: 75px;
height: 75px;
background: #b9cd6d;
}
#targetElement{
width: 300px;
height: 500px;
padding-top:50px;
}
</style>
<script>
$(function() {
// Position the dialog offscreen to the left, but centered
vertically
$( "#position1" ).position({
my: "center",
at: "center",
of: "#targetElement"
329
JQueryUI
});
$( "#position2" ).position({
my: "left top",
at: "left top",
of: "#targetElement"
});
$( "#position3" ).position({
my: "right-10 top+10",
at: "right top",
of: "#targetElement"
});
$( document ).mousemove(function( event ) {
$( "#position4" ).position({
my: "left+3 bottom-3",
of: event,
collision: "fit"
});
});
});
</script>
</head>
<body>
<div id="targetElement">
<div class="positionDiv" id="position1">Box 1</div>
<div class="positionDiv" id="position2">Box 2</div>
<div class="positionDiv" id="position3">Box 3</div>
<div class="positionDiv" id="position4">Box 4</div>
</div>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in
a standard browser which supports javascript, you must also see the following output.
Now, you can play with the result:
Box 1
Box 2
Box 3
330
JQueryUI
Box 4
In this example, we see that:
Box 2 is aligned to left top (horizontally and vertically) of the div element.
Box 3 is displayed in the top right corner of the window, but leave some padding
so that the message stands out more. This is done using the horizontal and vertical
values of my or at.
For Box 4, the of value is set as an event object. This is an event associated with
a pointer and moves with the mouse event.
331
JQueryUI
Earlier, the only way to write custom controls in jQuery was to extend the $.fn namespace.
This works well for simple widgets. Suppose you build more stateful widgets, it quickly
becomes cumbersome. To aid in the process of building widgets, Widget Factory was
introduced in the jQuery UI, which removes most of the boilerplate that is typically
associated with managing a widget.
The jQueryUI Widget Factory is simply a function ($.widget) that takes a string name and
an object as arguments and creates a jQuery plugin and a "Class" to encapsulate its
functionality.
Syntax
Following is the syntax of jQueryUI Widget Factory method:
Base Widget
Base widget is the widget used by the widget factory.
Options
The following table lists the different options that can be used with the base widget:
Option
Description
This option disables the widget if set to true. By default its value is false.
disabledhide Example
$( ".selector" ).widget({ disabled: true });
hide
JQueryUI
Boolean: If set to false no animation will be used. Element will fade out
with the default duration and the default easing if set to true.
Number: The element will fade out with the specified duration and the
default easing.
Object: If the value is an object, then effect, delay, duration, and easing
properties may be provided.
Example
show
Number: The element will fade in with the specified duration and the default
easing.
Object: If the value is an object, then effect, delay, duration, and easing
properties may be provided.
Example
333
JQueryUI
Methods
Following table lists the different methods that can be used with the base widget:
Action
Description
_create()
This
method
is
the
widget's
constructor. There are no parameters,
but [Link] [Link] are
already set.
_delay( fn [, delay ] )
_destroy()
_focusable( element )
_getCreateEventData()
_getCreateOptions()
334
JQueryUI
_hoverable( element )
_init()
This
method
the_setOptions()
individual option.
be updated based
is
called
from
method for each
Widget state should
on changes.
_setOptions( options )
_superApply( arguments )
destroy()
JQueryUI
enable()
option( optionName )
option()
option( options )
widget()
Events
Event Method
Description
This event is triggered when a widget is created.
Syntax
create( event, ui )
$( ".selector" ).widget({
create: function( event, ui ) {}
});
$( "#elem" ).progressbar();
336
JQueryUI
Changing widget options: For example
Example
Now let us create a custom widget in the following example. We will create a button widget.
We will see how to create options, methods and events in a widget in the following
examples :
JQueryUI
this._button.css("position", "absolute");
this._button.css("left", "100px");
$([Link]).append(this._button);
},
});
$("#button1").myButton();
});
</script>
</head>
<body>
<div id="button1"></div>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a
standard browser which supports javascript, you must also see the following output:
JQueryUI
this._button.css("background-color", [Link]);
this._button.css("position", "absolute");
this._button.css("left", "100px");
$([Link]).append(this._button);
},
_setOption: function(key, value) {
switch (key) {
case "width":
this._button.width(value);
break;
case "color":
this._button.css("background-color",value);
break;
}
},
});
$("#button2").myButton();
$("#button2").myButton("option", {width:100,color:"#cedc98"});
});
</script>
</head>
<body>
<div id="button2"></div>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a
standard browser which supports javascript, you must also see the following output:
this._button.css("position", "absolute");
this._button.css("left", "100px");
339
JQueryUI
Following this, the user can now call your method in the usual jQuery UI way:
this._button.css("position", "absolute");
this._button.css("left", "100px");
$("button3").myButton("move", 200);
<!doctype html>
<head>
<meta charset="utf-8">
<title>jQuery UI Widget - Default functionality</title>
<link rel="stylesheet"
href="//[Link]/ui/1.10.4/themes/smoothness/[Link]">
<script src="[Link]
<script src="[Link]
<script>
$(function() {
$.widget("[Link]", {
_create: function() {
this._button = $("<button>");
this._button.text("My first Widget Button");
this._button.width([Link])
this._button.css("background-color", [Link]);
this._button.css("position", "absolute");
this._button.css("left", "100px");
$([Link]).append(this._button);
},
move: function(dx) {
var x = dx+parseInt(this._button.css("left"));
this._button.css("left", x);
if(x>400){ this._trigger("outbounds",{},
{position:x}); }
}
});
$("#button3").myButton();
$("#button3").myButton("move", 200);
});
</script>
</head>
340
JQueryUI
<body>
<div id="button3"></div>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a
standard browser which supports javascript, you must also see the following output:
JQueryUI
{position:x}); }
}
});
$("#button4").myButton();
$("#button4").on("mybuttonoutbounds", function(e, ui) {
alert("out");
});
$("#button4").myButton("move", 500);
});
</script>
</head>
<body>
<div id="button4"></div>
</body>
</html>
Let us save the above code in an HTML file [Link] and open it in a
standard browser which supports javascript, an alert box opens up.
342
JQueryUI
343