Blog

Creating Elements in JavaScript, (any good?)

I’ve been playing around with a couple of ideas for JavaScript methods, thought I’d get some feedback.

The following link is a code snippet in PasteBin that shows the methods and a quick test:

http://pastebin.com/f7b68ab19

Basically, the two I wrote and that I’m evaluating are:

Create Element
This method caches a created element instance in an array. In some browsers, like Firefox, this method is marginally faster; I’m thinking the array access is hindering it from being even faster.
Create Element with Properties
This method basically allows you to set attributes using a JSON object. Nothing fancy, but the developers like it. I’m concerned about the speed of the loop, and developers have been asking about how to set the runtime style. Since element.style is, of course, not settable, I’d have to either expose a second parameter, or allow a more complex object and write more logic, (assuming I decide to support it).

I’d love some advice… here’s what I don’t want:

Just use jQuery / Prototype / my framework!

That kind of defeats the purpose of this exercise! :-)

1 Awesome Comments So Far

Don't be a stranger, join the discussion by leaving your own comment
  1. Jim
    March 25, 2009 at 6:23 pm #

    I like your post. However, I don’t see why you wouldn’t consider using a js framework…after all if you were you wouldn’t need to worry about the best way to do this since it would take care of it for you.

    Cheers,
    jim

Leave a Comment

Remember to play nicely folks, nobody likes a troll.