Form Reset Mixin


Form Reset Mixin

Description: A mixin to call refresh() on an input widget when the parent form gets reset.

QuickNavExamples

Options

Events

This only works for native input elements that have a form attribute, like an <input>. It doesn't work for other elements like <label> or <div>

Methods

_bindFormResetHandler()Returns: jQuery (plugin only)

Call this._bindFormResetHandler() inside _create() to initialize the mixin.
  • This method does not accept any arguments.
Code examples:

Set the background color of the widget's element based on an option.

1
2
3
_create: function() {
this._bindFormResetHandler();
}

_unbindFormResetHandler()Returns: jQuery (plugin only)

Call this._unbindFormResetHandler() inside _destroy() to destroy the mixin.
  • This method does not accept any arguments.
Code examples:

1
2
3
_destroy: function() {
this._unbindFormResetHandler();
}

Example:

Type colors into the input

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery.ui.formResetMixin demo</title>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.13.3/themes/smoothness/jquery-ui.css">
<style>
.demo-colorize-swatch {
width: 50px;
height: 50px;
border: 1px solid black;
}
</style>
<script src="https://code.jquery.com/jquery-3.7.1.js"></script>
<script src="https://code.jquery.com/ui/1.13.3/jquery-ui.js"></script>
</head>
<body>
<form>
<input id="colorize">
<button type="reset">Reset form</button>
</form>
<script>
$.widget( "custom.colorize", [ $.ui.formResetMixin, {
_create: function() {
this.swatch = $("<div>")
.addClass("demo-colorize-swatch")
.insertAfter(this.element);
this.refresh();
this._bindFormResetHandler();
this._on({ keyup: "refresh" });
},
refresh: function() {
this.swatch.css( "background-color", this.element.val() );
},
_destroy: function() {
this.swatch.remove();
this._unbindFormResetHandler();
}
} ] );
$( "#colorize" ).colorize();
</script>
</body>
</html>

Demo: