help with onclick submit

J

John

In an unnamed form, unnamed because it only submits an email address, I'm
trying to have the submit button clear the "e-mail" text value in the
textbox.

On the submit button I have the following:

onClick="if(this.form['e-mail'].value=='e-mail')this.form['e-mail'].value=''"

But it does not seem to work. Any ideas or suggestions?

Thanks in advance!
 
W

web.dev

John said:
In an unnamed form, unnamed because it only submits an email address, I'm
trying to have the submit button clear the "e-mail" text value in the
textbox.

On the submit button I have the following:

onClick="if(this.form['e-mail'].value=='e-mail')this.form['e-mail'].value=''"

But it does not seem to work. Any ideas or suggestions?

Thanks in advance!

You are accessing the form elements incorrectly. Try the following
instead:

this.form.elements['e-mail'].value
 
J

John

web.dev said:
You are accessing the form elements incorrectly. Try the following
instead:

this.form.elements['e-mail'].value

Thanks but it does not seem to work.

This here works:

<input type="text" name="email" value="e-mail"
onClick="if(this.value=='e-mail')this.value=''">

Then right below I have the line for my submit button as per your
suggestion:

<input type="submit" value="Go!"
onClick="if(this.form.elements['e-mail'].value=='e-mail')this.form.elements['e-mail'].value=''">

Unfortunately it's not working.

Any other ideas?

Thanks!
 
W

web.dev

John said:
web.dev said:
You are accessing the form elements incorrectly. Try the following
instead:

this.form.elements['e-mail'].value

Thanks but it does not seem to work.

This here works:

<input type="text" name="email" value="e-mail"
onClick="if(this.value=='e-mail')this.value=''">

Then right below I have the line for my submit button as per your
suggestion:

<input type="submit" value="Go!"
onClick="if(this.form.elements['e-mail'].value=='e-mail')this.form.elements['e-mail'].value=''">

Unfortunately it's not working.

Any other ideas?

Thanks!

Sorry for the confusion, when you first posted I had thought the name
of your input element was "e-mail". Try the same solution this time
replacing 'e-mail' with 'email':

this.form.elements['email'].value
 
J

John

web.dev said:
Sorry for the confusion, when you first posted I had thought the name
of your input element was "e-mail". Try the same solution this time
replacing 'e-mail' with 'email':

this.form.elements['email'].value

Thanks but it's still not working...
 
W

web.dev

John said:
web.dev said:
Sorry for the confusion, when you first posted I had thought the name
of your input element was "e-mail". Try the same solution this time
replacing 'e-mail' with 'email':

this.form.elements['email'].value

Thanks but it's still not working...

The current action is still submitting the form. The onClick even
expects a boolean value to be returned. To see the expected result you
are looking for, do the following:

<input type="submit" value="Go!"
onClick="if(this.form.elements['email'].value=='email'){this.form.elements['email'].value='';}
return false;">
 
J

John

web.dev said:
The current action is still submitting the form. The onClick even
expects a boolean value to be returned. To see the expected result you
are looking for, do the following:

<input type="submit" value="Go!"
onClick="if(this.form.elements['email'].value=='email'){this.form.elements['email'].value='';}
return false;">

Thanks, it makes sense, but it's still not working. I tried changing the
value from email to e-mail as that's the value in the previous statement is
but still nothing.
 
L

Lee

web.dev said:
web.dev said:
Sorry for the confusion, when you first posted I had thought the name
of your input element was "e-mail". Try the same solution this time
replacing 'e-mail' with 'email':

this.form.elements['email'].value

Thanks but it's still not working...

The current action is still submitting the form. The onClick even
expects a boolean value to be returned. To see the expected result you
are looking for, do the following:

<input type="submit" value="Go!"
onClick="if(this.form.elements['email'].value=='email'){this.form.elements['email'].value='';}
return false;">

Better:

Never, ever, use the onclick handler of a submit button.
Use the onsubmit handler of the form tag, instead.
 
M

Mick White

John said:
In an unnamed form, unnamed because it only submits an email address, I'm
trying to have the submit button clear the "e-mail" text value in the
textbox.

On the submit button I have the following:

onClick="if(this.form['e-mail'].value=='e-mail')this.form['e-mail'].value=''"

But it does not seem to work. Any ideas or suggestions?


What do you expect to happen when you click the submit button? You want
to submit the email value, don't you?
Mick
 
J

John

What do you expect to happen when you click the submit button? You want
to submit the email value, don't you?
Mick

Yes, but not if the email value == the initial value "e-mail"

In other words, I don't want an email address called "e-mail" to be
submitted so, so when they try to hit submit, the words "e-mail" go away
from the textfield.
 
J

John

Lee said:
Better:

Never, ever, use the onclick handler of a submit button.
Use the onsubmit handler of the form tag, instead.

Only problem with that is that the user never sees the text "e-mail"
disappear if it has to be submitted. Rather, I'd want them to see the text
disappear when they click on the submit button.This way, if the form is
submitted, then a blank address gets emailed instead of "email" being
submitted as an email address.
 
A

ASM

John a écrit :
web.dev said:
<input type="submit" value="Go!"
onClick="if(this.form.elements['email'].value=='email'){this.form.elements['email'].value='';}
return false;">

Thanks, it makes sense, but it's still not working. I tried changing the
value from email to e-mail as that's the value in the previous statement is
but still nothing.

<form action="foo.htm"
onsubmit="if(this.email.value=='e-mail'){
this.email.value='';
return false;
}
else
return true;">
<input type=text name="email" value="e-mail">
<input type=submit value=GO>
</form>
 
L

Lee

John said:
Only problem with that is that the user never sees the text "e-mail"
disappear if it has to be submitted. Rather, I'd want them to see the text
disappear when they click on the submit button.This way, if the form is
submitted, then a blank address gets emailed instead of "email" being
submitted as an email address.

Well, if you're not actually submitting anything, don't freaking use
a submit button. Use a regular button.
 
J

John

Well, if you're not actually submitting anything, don't freaking use
a submit button. Use a regular button.

It *is* submitting something, an email address. But if the email address is
"email" I want it blank.
 
J

John

ASM said:
<form action="foo.htm"
onsubmit="if(this.email.value=='e-mail'){
this.email.value='';
return false;
}
else
return true;">
<input type=text name="email" value="e-mail">
<input type=submit value=GO>
</form>


Thanks! :)
 
L

Lee

John said:
It *is* submitting something, an email address. But if the email address is
"email" I want it blank.

If you're submitting, and you want something to happen first, and/or
you want the submission to be cancelled under certain circumstances,
then use the onsubmit handler of the form. Do NOT use the onclick
handler of the submit button.
 
E

Evertjan.

ASM wrote on 29 sep 2005 in comp.lang.javascript:
<form action="foo.htm"
onsubmit="if(this.email.value=='e-mail'){
this.email.value='';
return false;
}
else

no need for the else here!
return true;">
<input type=text name="email" value="e-mail">
<input type=submit value=GO>
</form>

even shorter is:

onsubmit="
if(this.email.value!='e-mail')
return true;
this.email.value='';
return false;"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top