1. Markup

  2. Generating markup

    Old-school

  3. createElement

    Syntax

    document.createElement(tagName)

    Example

    document.createElement("p")

    Creates a documentFragment.

  4. createTextNode

    Syntax

    document.createTextNode(text)

    Example

    document.createTextNode("this is my text")
  5. Generating nodes

    Element nodes:
    createElement
    Text nodes:
    createTextNode
    Attribute nodes:
    setAttribute

    But how do you put them together?

  6. appendChild

    Syntax

    parentElement.appendChild(childNode)

    Example

    
    var para = document.createElement("p");
    var text = document.createTextNode("this is my text");
    para.setAttribute("title","my title");
    para.appendChild(text);
    

    This is still a documentFragment.

    <p title="my title">this is my text</p>
  7. Adding to the document

    Example

    
    var mydiv = document.getElementById("content");
    var para = document.createElement("p");
    var text = document.createTextNode("this is my text");
    para.setAttribute("title","my title");
    para.appendChild(text);
    mydiv.appendChild(para);
    

    The documentFragment has been added to the document:

    
    <body>
    <div id="content">
    <p title="my title">this is my text</p>
    </div>
    </body>
    
  8. insertBefore

    Syntax

    1. the new element you want to insert,
    2. the target element before which you want to insert it,
    3. the parent of both elements.
    parentElement.insertBefore(newElement,targetElement)

    Remember, you can always figure out the parent element:

    targetElement.parentNode
  9. insertBefore

    Example

    
    var mydiv = document.getElementById("content");
    var para = document.createElement("p");
    var text = document.createTextNode("this is my text");
    para.setAttribute("title","my title");
    para.appendChild(text);
    var container = mydiv.parentNode;
    container.insertBefore(para,mydiv);
    

    The documentFragment has been added to the document:

    
    <body>
    <p title="my title">this is my text</p>
    <div id="content">
    </div>
    </body>
    
  10. JavaScript Image Gallery Revisited

    Markup

    
    <ul id="imagegallery">
    <li><a href="images/andy.jpg">Andy</a></li>
    <li><a href="images/ian.jpg">Ian</a></li>
    <li><a href="images/doug.jpg">Doug</a></li>
    </ul>
    <img src="images/placeholder.jpg" alt="my gallery"
     id="placeholder" />
    

    View example (JS)

  11. insertAfter

    There is no insertAfter!

    Write your own:

    
    function insertAfter(newElement,targetElement) { 
      var parent = targetElement.parentNode; 
      if (parent.lastChild == targetElement) { 
        parent.appendChild(newElement); 
      } else { 
        parent.insertBefore(newElement,targetElement.nextSibling); 
      } 
    }
    

    View revised example (JS)

  12. Use with care

    Example: Displaying citations with blockquotes

  13. Summary

  14. Next...

    Maniplating styles