Giter VIP home page Giter VIP logo

element.write's Introduction

element.write

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.

Usage

elementWrite.toElement(document.getElementByid('id')).
	write('<p id=hi>Hello <').
	write('i>World!').
	close();

Result:

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

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?

elementWrite.toElement(document.getElementByid('id')).
	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.

element.write's People

Contributors

iamnoah avatar

Watchers

James Cloos avatar Bastian Gutschke avatar

Forkers

wasimv09

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.