jQuery in Action
202
CHAPTER 7
Extending jQuery with custom plugins
This example is only slightly more complicated than our initial example, but 
exhibits the following additional key concepts:
exhibits the following additional key concepts:
A parameter is passed that affects how the method operates.
Three jQuery commands are applied to the wrapped set by use of jQuery 
chaining.
chaining.
The new command can participate in a jQuery chain because it returns the 
wrapped set as its value.
wrapped set as its value.
The 
filter()
 command is used to ensure that, no matter what set of 
wrapped elements the page author applied this method to, only text fields 
are affected.
are affected.
How might we put this method to use?
 Often, when defining an online order form, we may need to allow the user to 
enter two sets of address information: one for where the order is to be shipped 
and one for the billing information. Much more often than not, these two
addresses are going to be the same; making the user enter the same information
twice decreases our user-friendliness factor to less than we'd want it to be.
and one for the billing information. Much more often than not, these two
addresses are going to be the same; making the user enter the same information
twice decreases our user-friendliness factor to less than we'd want it to be.
 We could write our server-side code to assume that the billing address is the 
same as the shipping address if the form is left blank, but let's assume that our 
product manager is a bit paranoid and would like something more overt on the
part of the user.
product manager is a bit paranoid and would like something more overt on the
part of the user.
 We'll satisfy him by adding a check box to the billing address that indicates 
whether the billing address is the same as the shipping address. When this box is 
checked, the billing address fields will be copied from the shipping fields and
then made read-only. Unchecking the box will clear the value and read-only sta-
tus from the fields.
checked, the billing address fields will be copied from the shipping fields and
then made read-only. Unchecking the box will clear the value and read-only sta-
tus from the fields.
 Figure 7.1 shows a test form in its before and after states.
The page for this test form is available in the file chapter7/test.setreadonly.html
The page for this test form is available in the file chapter7/test.setreadonly.html
and is shown in listing 7.4.