I keep getting this error saying that it cannot find my javascript function checkpw(). It's being called by onfocus.
<script type="text/javascript" >
function checkpw() {
alert ("working");
}
</script>
</head>
<body>
<h2>Welcome to our webpage.</h2>
<p>{{ reginfo}}</p>
<form action="/validate/" method ="get" >
<p>Username</p>
<input type="text" name="username" class="textbox"> </input > </br>
<p>Password</p>
<input class="textbox" name="password" id="password" type="text"> </input > </br>
<p>Confirm Password</p>
<input class="textbox" id="checkpw" type="text" onfocus="checkpw()"> </input > </br>
<p>Email<p>
<input class="textbox" name="email" type="text"> </input > </br>
<input type="submit" class="button" value="Submit">
</form>
</body>
I'm probably making a really stupid mistake but i'm new to javascript so anything that helps would be great. thanks.
2 Answers 2
Due to behavior that some ancient version of Internet Explorer implemented, in "quirks" mode, most browsers will let you directly address an element by its id.
E.g.
<div id="test"></div>
<script>
test.innerHTML = 'Hi';
</script>
I think this is what's happening for you. You have an element with id checkpw and also a function named checkpw. I think as the element is defined later on in the file, it is winning out, and since it's not a function, attempting to invoke it in your onfocus handler doesn't work.
Either change the name of your function, change the id of the element, or (more preferably) ensure that your page is not rendering in "quirks" mode (e.g. proper doctype, no invalid HTML, etc)
Comments
change: function checkpw() to: function checkPw()
and: focus="checkpw()" to: focus="checkPw()"
Edit:
It is not a reserved word; however the name is already polluted in the global namespace. As Domenic and jimr pointed out, the id attribute is using the same name, thus causing a conflicting condition.
The solution is still to change either:
(1) the id value of the input element
-or-
(2) the function name (as I stated above)
4 Comments
checkpw, changing it fixed it, but then I did some other testing and it did not work. Mind you, I'm testing in Safari at the moment.