Practical prototype and part 25: The information in this book is distributed on an "as is" basis, without warranty Although every pre-caution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. | 134 CHAPTER 6 WORKING WITH THE DOM Now append each cell to the row. tdl td2 td3 .and append the row to the table body. cities .down tbody .insert tr bottom Three times as many lines As is often the case one approach is easy but sloppy and the other is harder but more correct. Can t we split the difference First create the row. var tr new Element tr class total Next create each cell and append it on the fly. new Element td .update Total new Element td class number .update totalPopulation new Element td class code Now append the row to the table body. cities .down tbody .insert tr bottom We haven t talked about this technique yet but it ought to seem familiar anyway. The glue in the middle the new Element part takes a tag name as its first argument creates that element calls on it to give it the Prototype instance methods and then returns the element. The rest of it is stuff we ve already covered The optional second argument to Element is an object with the attribute value pairs the element should have. Element writeAttribute takes care of that part. Instead of the annoying we can use Prototype s own Ele-ment update to set the text content of a new element. The last step is the same in all cases. We use Element down to hop from the table to its tbody and then we use Element insert to place our new element after all the other rows. CHAPTER 6 WORKING WITH THE DOM 135 The wrap Method Let s take a time-out from the example to talk about Element wrap. Sometimes you ll want to create a new element to act as a container for something that s already on the page. This is one mode of attack for browser bugs rendering issues for example can sometimes be defeated with a wrapper element. Prototype s Element wrap is shorthand for creating an element and specifying its contents all at once. Its arguments are identical to those of the Element constructor .