ptg16476052
Modifying Content on the Page 511
18
In this example, I use the hasClass() method to determine whether the class is already
present. If it isn’t, I add it. If it is, I display the alert.
Manipulating Form Values
You can also use jQuery to modify the contents of form fields. The val() method can be
used to both retrieve the value of form fields and modify them. In many cases, websites
put an example of the input that should be entered into a form field in the field until the
user enters data. In the following example, the form starts with sample data in the field,
but it’s removed automatically when the user focuses on the field. If the user doesn’t
enter data, the sample data is restored. Figure 18.8 shows the initial state of the page.
<!DOCTYPE html>