Submitting A Form


S

Super Steve

I'm trying to write a little script that disables the submit button on
a form, changes the text of the button, and then submits the form.

Can anyone tell me why this works ok:

<input type="button" value="Submit" onClick="this.disabled=true;
this.value = 'Submitting...'; this.form.submit();">

But when I try to make a function it doesn't work:

<script language="JavaScript">
function DisableButton(b) {
b.disabled = true;
b.value = 'Submitting...';
b.form.submit();
}
</script>

<input type="button" value="Submit" name="submit"
onClick="DisableButton(this);">

When I try this the button text is changed and the button is disabled,
but the form doesn't submit. In IE I get an error that points to the
line that says:

b.form.submit();

and the error:

Object doesn't support this property or method

What's the difference between doing this code directly in the onClick
event and doing it in a function?
 
Ad

Advertisements

S

Sova.tm

because b=to the button not to the form the riht syntax should be:

window.document.FormName.submit();
 
R

Richard Cornford

Super said:
I'm trying to write a little script that disables the
submit button on a form, changes the text of the button,
and then submits the form.

Can anyone tell me why this works ok:

<input type="button" value="Submit" onClick="this.disabled=true;
this.value = 'Submitting...'; this.form.submit();">

But when I try to make a function it doesn't work:

<script language="JavaScript">
function DisableButton(b) {
b.disabled = true;
b.value = 'Submitting...';
b.form.submit();
}
</script>

<input type="button" value="Submit" name="submit"
onClick="DisableButton(this);">

When I try this the button text is changed and the button is
disabled, but the form doesn't submit. ...
<snip>

<URL: http://jibbering.com/faq/faq_notes/form_access.html#faComMis >

Richard.
 
S

Super Steve

Thanks for your reply. Initially I thought the same as you, but then I
read that every form element also had a "form" property that points to
the form that the element is an item of.

"All form element objects also have the form property, which returns
the Form object in which the element is contained. This can be useful
in cases where you have a generic routine that checks the validity of
data in a form. For example, when the user clicks a submit button, we
can pass the Form object referenced by the form property to our data
checker which can use it to loop through each element on the form in
turn, checking that data in the element is valid. This is handy if you
have more than one form defined on the page or where you have a generic
data checker that you cut and paste to different pages - this way you
don't need to know the form's name in advance."

So I don't know why b.form.submit.() doesn't work when b is pointing to
the button element within the form.
 
Ad

Advertisements

S

Super Steve

Awsome, Richard!

Well that's a trap for your players. I've always named my submit
buttons "submit" in the past, but then I've never tried to access them
with JavaScript before. Changing the name to "submitform" now makes
everything work.

Many thanks for your help!
 

Top