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

(singke) #1
ptg16476052

Workshop 551

19


$(this).html("Collapse All");
} else {
$("#faq dd").hide();
$(this).html("Expand All");
}
});


First, I add the new link to the page. I use the jQuery insertAfter() method, which adds
the content specified after the selector passed to it. Had the selector matched multiple ele-
ments, the content would have been appended after each of them.


After I add the new element to the page, I add a click handler for the link. It performs the
same functions as does the non-jQuery version I presented previously, but it uses jQuery
methods to accomplish the same thing with less code.


Summary


This lesson demonstrated a number of common tasks associated with programming in
JavaScript. It illustrated how to access the values in forms and check them for errors. It
also explained how you can manipulate the styles on a page and even the contents of a
page using JavaScript. The final two examples were written in a style referred to as unob-
trusive JavaScript, which involves writing JavaScript in such a way that the page still
works even if the user has disabled JavaScript or his browser does not offer JavaScript
support. JavaScript is used to enhance the user’s experience, but the functionality of the
page is not dependent on JavaScript. This approach is generally favored as the preferable
way to write JavaScript these days. It separates JavaScript code from the markup on the
page and ensures support for the largest number of users, including users with mobile
browsers that may not support JavaScript functionality.


Workshop


The following workshop includes questions, a quiz, and exercises related to the uses of
JavaScript.


Q&A


Q Can you point me in the direction of more scripts that I can integrate with my
pages?

A A number of sites provide JavaScript you can copy and paste for use on your own
pages, but this approach has fallen out of favor. Given the simplicity of building
functionality with jQuery and other libraries, the best bet is to find libraries that
provide the building blocks you need to write your own scripts and then put the
pieces together to accomplish your goals.



Free download pdf