1

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

3

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)

Sign up to request clarification or add additional context in comments.

Comments

0

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

It is not a reserved word or function.
You're right it's not. But I did find something weird happening on jsFiddle with 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.
The reason for the behavior is explained by @jimr's answer, which is correct and should be the accepted one.
Thanks Domenic, yes you both are right. I had the fix w/o the right reasoning

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.