MrFish 9 Posted July 10, 2009 Report Share Posted July 10, 2009 I keep getting caught on stupid things like this. I can't even get a onClick event to work. I have better things to do then spend hours trying to figure out why this perfect code doesn't do anything. <script type="text/javascript"> function login(){ alert('GOOOOO'); }</script><form name="loginform"><input type="text" name="username" value="Username" style="width: 150px" id="username"><br /><input type="password" name="password" value="password" style="width: 150px" id="password"><br /><input type="button" name="login" value="login" onclick="login()" style="width: 150px" ><br /><br /><input type="button" value="Signup" style="width: 200px;"></form> in debugging I went from an entire ajax login (which I've gotten to work before) down to a simple alert then won't work. Can you believe this? Quote Link to post Share on other sites
Ingolme 1,020 Posted July 10, 2009 Report Share Posted July 10, 2009 I keep getting caught on stupid things like this. I can't even get a onClick event to work. I have better things to do then spend hours trying to figure out why this perfect code doesn't do anything.<script type="text/javascript"> function login(){ alert('GOOOOO'); }</script><form name="loginform"><input type="text" name="username" value="Username" style="width: 150px" id="username"><br /><input type="password" name="password" value="password" style="width: 150px" id="password"><br /><input type="button" name="login" value="login" onclick="login()" style="width: 150px" ><br /><br /><input type="button" value="Signup" style="width: 200px;"></form> in debugging I went from an entire ajax login (which I've gotten to work before) down to a simple alert then won't work. Can you believe this? What browser are you using, and have you checked the error console to see what's going on? Quote Link to post Share on other sites
MrFish 9 Posted July 10, 2009 Author Report Share Posted July 10, 2009 What browser are you using, and have you checked the error console to see what's going on?I try all the browsers. I don't know how to use the console, or what it is. I'll look into it. Quote Link to post Share on other sites
Ingolme 1,020 Posted July 10, 2009 Report Share Posted July 10, 2009 Show all the code involved in your page. I think something might be interfering. Quote Link to post Share on other sites
dsonesuk 913 Posted July 10, 2009 Report Share Posted July 10, 2009 (edited) change function name login() to loginx() and it work, is login() reserved name? can't find it listed anywhere.it seems if the name="login" or id="login" matches the function name it won't work, strange... can't find anything that relates to this problem. Edited July 10, 2009 by dsonesuk Quote Link to post Share on other sites
jesh 0 Posted July 10, 2009 Report Share Posted July 10, 2009 it seems if the name="login" or id="login" matches the function name it won't work, strange... can't find anything that relates to this problem.Also note this - the following will execute the alert:<script type="text/javascript">function login(){ alert('GOOOO');}</script><input type="button" name="login" onclick="login();" /> Whereas this will not: <script type="text/javascript">function login(){ alert('GOOOO');}</script><form><input type="button" name="login" onclick="login();" /></form> So, as dsonesuk suggests, keep your function names different than your element names and you won't run into this problem again. Quote Link to post Share on other sites
justsomeguy 1,135 Posted July 10, 2009 Report Share Posted July 10, 2009 I think this is a scope issue. With the form tags there, the scope of all form elements is the form itself. So when one of the form elements is using the name of a form element (even itself), it's being scoped to the form so that's what Javascript is looking at. You could probably execute the function in the second example if you used window.login instead of just login. It might also work if you moved the script block inside the form also. I'm not sure if that would define the function in the scope of the form, I don't think it would. The first piece of code works because, since the input is not inside a form, the scope that the button is defined in is the same as the scope that the function is defined in, so it might be that the function definition is taking precedence over the named input. That doesn't even come into play when the form is there because they're in two different scopes. Quote Link to post Share on other sites
MrFish 9 Posted July 10, 2009 Author Report Share Posted July 10, 2009 (edited) Ah yes, when changing the function name it DID work. Very happy about this, thanks Edited July 10, 2009 by MrFish Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.