...

View Full Version : JS: Form Reference Question (Quick and Easy)



kraftomatic
08-14-2008, 05:22 PM
Hey Guys,

Having a senior moment here. I have the following JS reference to a form:

document.forms['formLogin'].submit();

I want to use this code across other pages with different form names, so what's the easiest way to grab the form name from the page, and reference it in the above line of code? Something close to:

var formName = this.form; (this doesn't work)
document.forms[formName].submit();


Any suggestions would be appreciated.

Thanks.

Philip M
08-14-2008, 05:50 PM
Why you don't use the submit method of the form object?

<input type = "text" name="txt1" size = "20" onchange="this.form.submit()">

or whatever

kraftomatic
08-14-2008, 06:49 PM
Because I'm doing some field checking in the form before I submit it ..

Philip M
08-14-2008, 07:48 PM
The general idea is:-


<form name = "myform" method = "POST" action = "url">
<input type = "text" name="txt1" id = "txt1" size = "20" onchange = "if (validate(this)) {this.form.submit()}">
</form>

<script type = "text/javascript">
function validate(which) {
x = which.value;
if (x != "Philip") {
return false;
}
return true;
}
</script>

Cranford
08-14-2008, 08:00 PM
Use:

document.forms[0].submit;

The first set of <form></form> tags, reading the code from top to bottom will always be the ordinal number zero.

You can address a form, and other tag sets, either by name or ordinal number.

If the "name" of the first form is, for example, "user_login", then the following are equivalent.

document.forms[0].submit();
document.forms['user_login'].submit();

kraftomatic
08-14-2008, 08:04 PM
Use:

document.forms[0].submit;

The first set of <form></form> tags, reading the code from top to bottom will always be the ordinal number zero.

You can address a form, and other tag sets, either by name or ordinal number.

If the "name" of the first form is, for example, "user_login", then the following are equivalent.

document.forms[0].submit();
document.forms['user_login'].submit();

Thanks Cranford. I was looking to grab the name of the form, but the numerical reference was one I had forgotten but will work.

Cheers.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum