469,579 Members | 1,112 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,579 developers. It's quick & easy.

Calling a function from 2 different forms on different pages

I have this function below

function checkLength(field, nextField)
{
var letters = document.form1.elements[field].value.length +1;
if (letters <= 2)
{document.form1.elements[field].focus()}
else
{document.form1.elements[nextField].focus()}
}

And i call it from here:
<form name="form1" method="POST" action="addComputer.php"
ONSUBMIT="return ValidateForm(this)">
Hardware Ethernet
<input type="text" name="hardwareEtherneta" maxlength=2 size=1
ONKEYUP="checkLength('1','2')">
</form>

My problem is that I have another form on a different page:
<form name="form2" method="POST" action="addComputer.php"
ONSUBMIT="return ValidateForm(this)">
Hardware Ethernet
<input type="text" name="hardwareEtherneta" maxlength=2 size=1
ONKEYUP="checkLength('1','2')">
</form>

When the call to check length from form2, it fails as document.form1
isn't an object. How do I need to alter the function keeping the 2
form names different to get the function to work ok?
Help greatly appreciated, sorry newbie!!!!
Jul 20 '05 #1
1 1484
Bryan wrote:
I have this function below

function checkLength(field, nextField)
{
var letters = document.form1.elements[field].value.length +1;
if (letters <= 2)
{document.form1.elements[field].focus()}
else
{document.form1.elements[nextField].focus()}
}

And i call it from here:
<form name="form1" method="POST" action="addComputer.php"
ONSUBMIT="return ValidateForm(this)">
Hardware Ethernet
<input type="text" name="hardwareEtherneta" maxlength=2 size=1
ONKEYUP="checkLength('1','2')">
</form>

My problem is that I have another form on a different page:
<form name="form2" method="POST" action="addComputer.php"
ONSUBMIT="return ValidateForm(this)">
Hardware Ethernet
<input type="text" name="hardwareEtherneta" maxlength=2 size=1
ONKEYUP="checkLength('1','2')">
</form>

When the call to check length from form2, it fails as document.form1
isn't an object. How do I need to alter the function keeping the 2
form names different to get the function to work ok?
Help greatly appreciated, sorry newbie!!!!


simplest way is to simply tell it the formname:

onkeyup="checkLength('form1','thisFieldName','next FieldToFocus')"

function checkLength(formName,thisField, nextField)
{
letters = document.forms[formName].elemenets[thisField].value.length +1;
if (letters <= 2)
{document.forms[formName].elements[thisField].focus()}
else
{document.forms[formName].elements[nextField].focus()}
}

Could still be made a lot simpler, by passing references to the form and
the current field.

onkeyup="checkLength(this.form,this,'nextFieldName ')"

I will leave it to you to try to code the second one.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Jon Hyland | last post: by
7 posts views Thread by Kenneth Brody | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.