cycling jQuery_Succinctly | Page 21

Breaking the chain with destructive methods As mentioned before, not all jQuery methods maintain the chain. Methods like text() can be chained when used to set the text node of an element. However, text() actually breaks the chain when used to get the text node contained within an element. In the example below, text() is used to set and then get the text contained within the

element. Sample: sample7.html

Getting the text contained within an element using text() is a prime example of a broken chain because the method will return a string containing the text node, but not the jQuery wrapper set. It should be no surprise that if a jQuery method does not return the jQuery wrapper set, the chain is thereby broken. This method is considered to be destructive. Using destructive jQuery methods and exiting destruction using end() jQuery methods that alter the original jQuery wrapper set selected are considered to be destructive. The reason is that they do not maintain the original state of the wrapper set. Not to worry; nothing is really destroyed or removed. Rather, the former wrapper set is attached to a new set. However, fun with chaining does not have to stop once the original wrapped set is altered. Using the end() method, you can back out of any destructive changes made to the original wrapper set. Examine the usage of the end() method in the following example to understand how to operate in and out of DOM elements. 21