There is no rest for the Prototype core team. Immediately after we pushed version 1.5.1 out, great changes continue to incubate in our experimental SVN branches, and some of those have recently made their way into trunk. Here’s a look at one such change that will make creating DOM nodes simpler in the next release of Prototype.

Recently the DOM part of the framework has received much love from Tobie Langel, who hacked around the DOM branch a lot to provide us with great new features for the next release.

Today’s highlight is that now you can create a new HTML element like this:

var form = new Element('form', {
                         action: "/user/create",
                         method': "post"
                       });
//-> <form action="/user/create" method="post">

That’s right – you can specify both the element and its attributes in a single call! So, you’ve got your own minimal DOM builder right here. Naturally, this is entirely cross-browser since it maps the attributes to correct properties internally. You can even do this in IE without tears:

new Element('input', { name: "user", disabled: true });
//-> <input name="user" disabled="disabled" />

Experienced coders will remember that Internet Explorer doesn’t allow the name attribute to be set in a normal way. Well, we worked around that, too. This also demonstrates how boolean true as an attribute value results in repeating its name inside the value (disabled="disabled"), which is a convention of XHTML.

The Element constructor demonstrated here sets attributes by using a method that’s also new: Element#writeAttributes. We should also mention that the constructor is optimized for speed when creating multiple instances of the same type. Using document.createElement() a lot to create nodes you will later manipulate with Prototype? You should use the Element constructor instead.

This, folks, is currently in the trunk, but you can check it out with SVN (instructions) and use anytime. You can also download this development build if you want to skip the SVN process. Big thanks to Martin Ström and Tom Gregory for inspiring this and helping it happen. Also thanks everyone participating in the discussion for helping us make Prototype a better library!