div = document.createElement('div')
div.update('<p>I like prototype</p>')

The only place I see this not working is in naughty browsers, like IE, but this seems to fix it:
div = document.createElement('div')
div = $(div)
div.update('<p>I like prototype</p>')

Maybe I'm not reading this correctly?

Justin Perkins said on September 06, 2006

My html wasn't encoded, but you get the idea.

Andrew Dupont said on September 06, 2006

Justin: Element.extend is technically only needed for IE and any other browser that won't let you modify HTMLElement.prototype. This is why your first example works in Firefox.

For the sake of IE compliance you have to explicitly "extend" DOM nodes, using either Element.extend or $ (which extends anything it returns). I prefer the latter because it's shorter.

Note that getElementsByClassName and $$/getElementsBySelector will also extend any nodes they return.

Justin Perkins said on September 06, 2006

Yes I'm quite familiar with IE's reluctance to do this, but didn't really understand the *why*.

If you asked me 6 months ago, I would have told you to use calls to $() sparingly, but I'm now finding myself calling it on all the time for the very reason you stated above.

I figured $() and Element.extend() were mostly the same, but still, I've never even heard of extend() until now.

Jonathan Snook said on September 06, 2006

$() and Element.extend are very similar in that $() will automatically extend the element. But, it prevents a small overhead (the dollar function doesn't have to enter a loop for one element and push that one element onto an array to be returned back) and makes the code easier to understand.

ZhetoN said on October 17, 2008

I usually use

	var element=document.createElement(elm);
	for(var key in this)
	return element;
Sorry, comments are closed for this post. If you have any further questions or comments, feel free to send them to me directly.