MrFish Posted July 10, 2009 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? Link to comment Share on other sites More sharing options...
Ingolme Posted July 10, 2009 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? Link to comment Share on other sites More sharing options...
MrFish Posted July 10, 2009 Author 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. Link to comment Share on other sites More sharing options...
Ingolme Posted July 10, 2009 Share Posted July 10, 2009 Show all the code involved in your page. I think something might be interfering. Link to comment Share on other sites More sharing options...
dsonesuk Posted July 10, 2009 Share Posted July 10, 2009 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. Link to comment Share on other sites More sharing options...
jesh Posted July 10, 2009 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. Link to comment Share on other sites More sharing options...
justsomeguy Posted July 10, 2009 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. Link to comment Share on other sites More sharing options...
MrFish Posted July 10, 2009 Author Share Posted July 10, 2009 Ah yes, when changing the function name it DID work. Very happy about this, thanks Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.