Shortening document.getElementById

You must be really fed up when you need to use document.getElementById() again and again in your JS code. jQuery provides a good variant by letting you use $(el). But, if you don’t wanna use jQuery, here is a method you can use to ease out you code by simplifying document.getElementById(), document.getElementsByName(), document.getElementsByTagName() or document.getElementByClassName().

All you need to so is make a function like this.

function getId(elm){return document.getElementById(elm);}

That was it! Now, you can use just getId() instead of document.getElementById(). Exempli gratia –

getId(‘id-of-some-element’).innerHTML = ‘This is awesome!’;

^ The above code changes the innerHTML of the element to ‘This is awesome!’.

Similarly, you can make functions like getName(), getClass() or getTag() to simplify other selectors. You can even use cool function names like $(), so that it looks like $(‘id-of-some-element’), just like jQuery does it!

Summer in code

It’s been long since I have blogged.

I played with a lot of code this summer. While most of my works are on the go to becoming startups, I also did some passtime code work. Here is a list of the ones that interested me the most –

1. Shrink – It is a lite URL shortener, that has damn simple style, and is rich in functionality. Shortens URLs fast with on-the-page shortening, stats, QR code generation, auto further redirection and more. Read the Features section for more details. Make an account and get the URL listing feature.

2. WordIt – A JavaScript app that displays character-related stats.

3. Tiper – Tiper is a Javascript based web app that types out the text input.

4. Inliner – Inliner is a web app that arranges your text input into one single line.

5. Reverzer – Reverzer is a web app that reverses your text input.