Web Development with jQuery®

(Elliott) #1

Summary (^) ❘ 133
Finally, the fi rst element is set to focus automatically, so you don’t have to click it to enter the
new album’s data.
tr.children('td:first').focus();


Summary


In this chapter, you learned about a variety of jQuery’s manipulative capabilities. The content dis-
cussed in this chapter is documented in detail in Appendix E, “Manipulating Content, Attributes,
and Custom Data,” and Appendix F, “More Content Manipulation.” You began this chapter by
learning about jQuery’s attribute manipulation method attr(), which enables you to specify attri-
butes in a variety of ways, with the attribute as the fi rst argument, and the value as the second, or
via an object literal specifying arguments in key-value pairs, and also by using callback functions.
jQuery’s removeAttr() method can be used to remove attributes completely.

You also learned how jQuery helps you immensely with manipulating class names. jQuery’s
addClass() method can add a class name to an element. Its hasClass() method can determine
whether a class name is present. Its removeClass() method can remove a class name. And its
toggleClass() method can toggle a class name on and off.

You learned about various jQuery methods used to manipulate text and HTML content. You can
get or set text or HTML content for elements using jQuery’s text() and html() methods. You can
append or prepend HTML content to other elements using jQuery’s append() or prepend() methods.
jQuery’s after(), before(), insertAfter(), and insertBefore() methods can all insert content beside
other content. And jQuery’s wrap(), wrapAll(), and wrapInner() methods can wrap elements with
wrapper elements. In addition, the unwrap() method can remove a parent element.

jQuery’s replaceWith() and replaceAll() methods can completely replace one or more elements
with other content. Its empty() method can completely remove an element’s children and descen-
dants. Its remove() methods can remove an element and all its contents from the document. And its
clone() method can duplicate content, and optionally, event handlers that exist within that content.

EXERCISES



  1. Write sample code that might be used to set both the value and the class attributes of an


element.

  1. If you want to set the href attribute for an element to http://www.example.com using jQuery, what


might the JavaScript look like?



  1. What jQuery method is used to completely remove attributes from elements?

  2. What jQuery method would you use to determine whether a class name is present for an


element?


http://www.it-ebooks.info

Free download pdf