Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

3. Events, Handlers and All that Jazz: A... > Adding an element to the DOM

Adding an element to the DOM

  1. Set up a handler to handle the user’s click

  2. Write the handler to get the song name

  3. Create a new element to hold the new song

  4. Add the element to the page’s DOM

To add a new element to the DOM you have to know where you want to put it. Well, we do know where to put it: we’re going to put the <li> element in the <ul> element. But how do we do that? Let’s take another look at the DOM. Remember how we said it was like a tree? Think family tree:

image with no caption

So, to add our <li> element, we need to make it a child of the <ul> element. To do that, we first need to find the <ul> element in the tree (we gave it an id of “playlist” to make that easy) and then to add the <li>, we tell the <ul> element to add a new child to itself. Here’s how we do that:


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial