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

Chapter 4. Altering Lists > Inserting Line Items into Specific Positions

Inserting Line Items into Specific Positions

Let's say we want to insert a line item not at the end of a list, but into the third position. Example 4-4 shows the code to accomplish this.

Example 4-4. Inserting a line item into the third position

function insertItem()
{
    listDiv = document.getElementById("list")
    listElem = listDiv.getElementsByTagName(listType)[0]
    listItemToBump = listElem.childNodes[2]

    newListItem = document.createElement("LI")
    newListItem.innerHTML = document.listForm.todo.value
    listElem.insertBefore(newListItem, listItemToBump)
}


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint