Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make alias to document.getElementById in Javascript

How can I alias the function "document.getElementById" I've seen it done using $

Thanks

like image 496
Supernovah Avatar asked Jun 05 '09 05:06

Supernovah


3 Answers

The new features introduced in ES6 let us improve on Chris Lloyd's answer, simplifying:

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

to:

const $ = id=> document.getElementById(id);
like image 113
machineghost Avatar answered Nov 19 '22 00:11

machineghost


var $ = document.getElementById.bind( document );
like image 29
Adria Avatar answered Nov 19 '22 00:11

Adria


Possibly the easiest/shortest way:

function $(id) { return document.getElementById(id); };
like image 10
Chris Lloyd Avatar answered Nov 18 '22 23:11

Chris Lloyd