Sams Teach Yourself HTML, CSS & JavaScript Web Publishing in One Hour a Day

(singke) #1
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>




Altering Form Values




FIGURE 18.7
An alert is dis-
played when users
click a paragraph
the second time.

FI GURE 18.8
When the page
loads, the sample
content appears in
the form field.

Free download pdf