Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Like document.write, but for elements, and not evil.
branch: master

Merge pull request #4 from matthewcieplak/master

Allowing HTML5 data- attributes in elements
latest commit 6b98eb1848
@iamnoah authored


element.write is like document.write, but for DOM elements. The core is John Resig's pure JavaScript HTML parser but it's been refactored so that incomplete HTML fragments can be submitted but still generate parse events, similar to the way browsers parse HTML and generate the DOM.


    write('<p id=hi>Hello <').


<div id="id">
    <p id="hi">Hello <i>World</i></p>

Ummm, so? You've reinvented innerHTML

Not exactly, but if you don't see a use case for it in your web apps, I'm not surprised. elementWrite is a library for libraries.

So how is this different from the original HTML parser?

    write('<p id=hi>Hello <'). # 1
    write('i>World!'). # 2
    close(); #3
  1. At this point the DOM looks like this: <div id="id"><p>Hello </p></div>. i.e., the p tag and text are already in the DOM.
  2. This completes the i tag and adds World.
  3. If you used innerHTML for both of these fragments, you might get something like: <p id="hi">Hello </p>&lt;i&gt;World!, since innerHTML assumes you're giving it a complete fragment.

So basically element.write gives you the power to pause the HTML->DOM process at any arbitrary point and manipulate the DOM elements that are available.

See the tests for more interesting examples.

Something went wrong with that request. Please try again.