I created some functions in js, something very simple, and instead of calling these functions directly in the html I decided to execute them looking for the input id.
It was like this:
...
functions here
...
function id(el){
return document.getElementById(el);
}
window.onload = function(){
id('name').onkeyup = function(){inicialMaiuscula(this);}
id('phone').onkeyup = function(){mascara(this, mtel);}
id('postalcode').onkeyup = function(){mascara(this, cepm);}
}
The problem is that this js file is loaded on different pages, so if the input with that id
doesn't exist it returns the error:
can't access property "onkeyup", id(...) is null
And also on some pages the function works and on others it doesn't. Tips on how I can get around this?
I take the opportunity to ask another question, there are inputs that use the same function. How can I declare both id
on the same line without having to repeat all the code?