<!DOCTYPE html><html lang="en"><head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Widget - Default functionality</title>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.14.2/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
.custom-colorize-changer {
<script src="https://code.jquery.com/jquery-3.7.1.js"></script>
<script src="https://code.jquery.com/ui/1.14.2/jquery-ui.js"></script>
// the widget definition, where "custom" is the namespace,
// "colorize" the widget name
$.widget( "custom.colorize", {
// add a class for theming
.addClass( "custom-colorize" );
this.changer = $( "<button>", {
"class": "custom-colorize-changer"
.appendTo( this.element )
// Bind click events on the changer button to the random method
this._on( this.changer, {
// _on won't call random when widget is disabled
// Called when created, and later when changing options
this.element.css( "background-color", "rgb(" +
this.options.green + "," +
// Trigger a callback/event
this._trigger( "change" );
// A public method to change the color to a random value
// can be called directly via .colorize( "random" )
random: function( event ) {
red: Math.floor( Math.random() * 256 ),
green: Math.floor( Math.random() * 256 ),
blue: Math.floor( Math.random() * 256 )
// Trigger an event, check if it's canceled
if ( this._trigger( "random", event, colors ) !== false ) {
// Events bound via _on are removed automatically
// revert other modifications here
// remove generated elements
.removeClass( "custom-colorize" )
.css( "background-color", "transparent" );
// _setOptions is called with a hash of all options that are changing
// always refresh when changing options
_setOptions: function() {
// _super and _superApply handle keeping the right this-context
this._superApply( arguments );
// _setOption is called for each individual option that is changing
_setOption: function( key, value ) {
// prevent invalid color values
if ( /red|green|blue/.test(key) && (value < 0 || value > 255) ) {
this._super( key, value );
// Initialize with default options
$( "#my-widget1" ).colorize();
// Initialize with two customized options
$( "#my-widget2" ).colorize({
// Initialize with custom green value
// and a random callback to allow only colors with enough green
$( "#my-widget3" ).colorize( {
random: function( event, ui ) {
// Click to toggle enabled/disabled
$( "#disable" ).on( "click", function() {
// use the custom selector created for each widget to find all instances
// all instances are toggled together, so we can check the state from the first
if ( $( ":custom-colorize" ).colorize( "option", "disabled" ) ) {
$( ":custom-colorize" ).colorize( "enable" );
$( ":custom-colorize" ).colorize( "disable" );
// Click to set options after initialization
$( "#green" ).on( "click", function() {
$( ":custom-colorize" ).colorize( "option", {
<div id="my-widget1">color me</div>
<div id="my-widget2">color me</div>
<div id="my-widget3">color me</div>
<button id="disable">Toggle disabled option</button>
<button id="green">Go green</button>