create();
FormField.prototype = {
initialize: function(id) {
var el = $(id);
Event.observe(el,'focus',(function()
CHAPTER 4 n LIBRARIES 88
{
if(this.value == this.defaultValue) this.value = '';
}).bindAsEventListener(el));
Event.observe(el,'blur',(function()
{
if(this.value == '') this.value = this.defaultValue;
}).bindAsEventListener(el));
}
};
new FormField('searchfield');
This example used the Prototype method of creating a class and then attaching the
initialize() function to the prototype by specifying it as a method in the object literal. When
a new object is instantiated from it, the initialize() function is automatically run. The focus
and blur events are observed. For the focus event, if the value contained within is the default
value, it will clear the input, enabling the user to type from scratch. Then, on blur, the field is
reset to the default value if the field is blank.
jQuery
jQuery (http://jquery.com) is quick and nimble, and it was the first library that really highlighted
the power of method chaining. The library is well encapsulated and is guaranteed to
play well with other libraries using its own jQuery namespace. It offers a dollar sign function
that maps to an internal method. If you are using jQuery alongside Prototype or another
library that makes use of the dollar function, you can turn it off in jQuery.
jQuery is compact, yet extremely powerful.
Pages:
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151