Book
Of Zeus
Articles, Tutorials & Source Code
About
Contribute
© 2011-2025 Book of Zeus
All articles, code or tutorials listed on bookofzeus.com/ can be used as reference, links or as in a sharing matter without attribution.
You cannot copy whole tutorials (unless permission is given), either translating to another language.
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
By using and/or reading this site, you agree to our privacy policy and terms and conditions .
Create an alias to document getelementbyid in JavaScript
document.getElementById is pretty useful when developing Web 2.0 websites. But, writing over and over "document.getElementById" can be annoying. There is a simple way to fix that problem.
Creating an alias is a good way to simplify your code.
this.$ = function(elem) {
if(typeof(elem) != "string") {
return false;
}
if(!document.getElementById(elem)) {
return false;
}
return document.getElementById(elem);
}
Now, if you want to reference an HTML tag using the ID, you simply need to use the $
$('info');
instead of:
document.getElementById('info');