Grabbing elements from the DOM

If, like me, you actually quite enjoy working with the vanilla DOM implementations, you may wonder how you’re actually supposed to find the elements in the first place. I’ve been using quite a nice technique for a while now that allows you to have hyphenated names in your HTML, but keep camel case in your JavaScript even though you’re searching for the elements en masse.

What the end result looks like

When invoking my little snippet, you will use something like this.

Which will leave you with a neatly formatted object for you to work with. Nice ids, nice JavaScript. It’s a win-win.

As you can see, it’s magically converted the names to camel case to stop our eyes from witnessing any hyphenated JavaScript atrocities.


The first part of my little snippet is a function that converts a hyphenated string to camel case. This will be used within the function that goes off to find all of the elements.

All this will do is convert our names.

  • hello-world becomes helloWorld
  • login-form becomes loginForm
  • but-i-am-le-tired becomes butIAmLeTired

Pretty slick, right?

Fetching the elements

And now for the actual function. It will simply iterate over an array of IDs and insert them into an object with camel case names instead of hyphenated.

That’s all there is to it. You can now fetch your elements easily using nice HTML naming conventions, then access them from within your JavaScript under a namespace with camel case names. Bonus: Here’s a live copy on JSFiddle for you to play around with.