Shorthand for getElementById in Javascript andrey четверг, 3 апреля 2014 г. No Comment

Some times I feel lazy to use document.getElementById('selector_id') when I'm working with plain Javascript.

I can hear You also feel the same. Lets we can create our own shorthand for getElementById().

All we need is to just write a function for that.

var $ = function (id) {
return document.getElementById(id);
};

Here after We can call our get our element by ID like this,
$('btn')

I think you want to see it in DEMO... You can find it below...

NOTE : Most of the javascript libraries are using dollar sign ($) as short hand. So, you can use any character or word instead of '$' as a shorthand.


Some times I feel lazy to use document.getElementById('selector_id') when I'm working with plain Javascript.

I can hear You also feel the same. Lets we can create our own shorthand for getElementById().

All we need is to just write a function for that.

var $ = function (id) {
return document.getElementById(id);
};

Here after We can call our get our element by ID like this,
$('btn')

I think you want to see it in DEMO... You can find it below...

NOTE : Most of the javascript libraries are using dollar sign ($) as short hand. So, you can use any character or word instead of '$' as a shorthand.


by Jillur Rahman

Jillur Rahman is a Web designers. He enjoys to make blogger templates. He always try to make modern and 3D looking Templates. You can by his templates from Themeforest.

Follow him @ Twitter | Facebook | Google Plus

No Comment