Dollar E: A document.createElement Wrapper
2006-03-29 17:11 - Programming
First, a lead in: I work for Root Markets, as part of the Root Vaults team. The CEO of my company, Seth Goldstein presented at this year's ETech conference, and my team went through a lot of work to get some spiffy new features built for our site to demo at that conference.
The primary new feature ( from my perspective ;-) ) was a brand new UI that made use of AJAX, which I built on top of the Prototype JavaScript library. There was a lot of dynamic element creation based on the data coming back from the AJAX calls, and thus a lot of document.createElement calls, along with the appendChild and insertBefore and the element-property-setting statements. It's very verbose.
At one point early on, I decided to write a wrapper function for document.createElement to avoid typing that out so often, and ended up realizing I should reduce more typing by figuring out a way to pass in attributes like the class and ID to be set. And then I realized, and it feels obvious in retrospect, that it should be able to handle creating a whole tree. I'm sure I'm not the first person to think of this, or even to write such a function. But I have found this to be very useful, and so after getting permission to do so from my company, I came here to release it as open source.
I called the function $E() or "Dollar E". I see it as the reverse of the $() function in prototype. The $() function gets an element that exists, the $E() function creates a new element that doesn't. It is passed a custom data structure, and it returns a (tree of) DOM element(s). Here's the source code and an example, and the HTML string produced by running the example.
2006-03-29 23:32 - lm_mario