ptg16476052
514 LESSON 18: Using jQuery
I only want to let users click the Submit button if they’ve already entered an e mail
address. To add that check, I need to add a bit of code to the blur event for the email
field, as shown:
$("input[name='email']").blur(function () {
if ($(this).val() == "") {
$(this).val("[email protected]");
$(this).css("color", "#999");
$("#emailFormSubmit").attr("disabled", "disabled");
}
else {
$("#emailFormSubmit").removeAttr("disabled");
}
});
If the user leaves the field having set a value, the disabled attribute is removed from
the Submit button, as shown in Figure 18.11. If the user leaves th e field without having
entered anything, the disabled attribute is added, just in case it was previously removed.
Adding and Removing Content
jQuery provides a number of methods that can be used to manipulate the content on
the page directly. Here’s a more complex example that demonstrates several ways of
manipulating the content on a page—users can add new content to the page, remove co n-
tent from the page, and even wipe out all the content inside an element in one click. The
initial page appears in Figure 18.12.
FIGURE 18.11
The Submit but-
ton is no longer
disabled after an
email address is
entered.