Chapter 8

The DOM and Event Handling

IN THIS CHAPTER

  • Introducing the DOM
  • Appending elements using the DOM
  • Replacing elements using the DOM
  • Creating an Ajax shopping cart
  • Handling Ajax timeouts

This chapter is all about working in-depth in Ajax applications. In this chapter the Document Object Model (DOM) and handling events are described. A huge part of Ajax programming involves working in the Web page, of course, and the DOM lets you do that kind of work. Using the DOM you can append elements to what's already there, replace elements, search for and find elements, and so on. Event handling is also important when you're working with Ajax, and in this chapter you're going to see how to handle events to create a drag-and-drop Ajax-enabled shopping cart.

This chapter starts with an introduction to the DOM, which gives you access to all the parts of a Web page in JavaScript code.

Introducing the DOM

Your Web browser sees an HTML document as a tree of nodes that support properties and methods you can use to navigate or edit that page in real time. Those properties and methods are specified in the HTML Document Object Model, or DOM.

images The DOM is the creation of the World Wide Web Consortium (W3C). For the rigorous DOM details, take a look at http://www.w3.org/DOM/.

For example, here's the code for an innocent-looking Web page:

<html>
  <head>
    <title>
      Hello from HTML
 </title> </head> ...

Get Ajax Bible now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.