Jump to content
Sign in to follow this  
MrFish

What The %$#@?!

Recommended Posts

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? :)

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Show all the code involved in your page. I think something might be interfering.

Share this post


Link to post
Share on other sites

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 by dsonesuk

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Ah yes, when changing the function name it DID work. Very happy about this, thanks :)

Edited by MrFish

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...