I'm guessing you have that script in the <head>
tag. Place it just before the end of <body>
. Remember, browsers read from top to bottom, so if you try to select an element with the id of customername
before the browser has found it, it will return null
.
Alternatively, you could only call the function when the page has finished loading with:
document.addEventListener('DOMContentLoaded', checkName);
Which will only run checkName
when all of your HTML has been parsed (including the element with the id of customername
).