I would like to call some functions by shorter alias in order to minimize code size.
(function(){
var t = document.getElementById;
t('element-id');
})();
This piece of code gives Error: Could not convert JavaScript argument. Why?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
When you assign a function to a different variable, it’s
thisvalue changes. SincegetElementByIdexpectsthisto be an element, you’re getting an error.If you’re in an environment where you can use
bind, use it:This’ll ensure that
t‘sthiswill stay thedocumentobject.If you can’t use
bind, you’ll have to create an intermediary function: