473,396 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1613
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Simula | last post by:
I am developing an HTML javascript application and I want to preserve state in a way that can be book-marked. I chose HTML anchors as a means of preserving state. When the application changes...
10
by: headware | last post by:
I know that you can call the method of one from from inside another form by doing something like this Forms("MyForm").MyFunction(12, 34) However, you have to know that MyForm has a function...
2
by: Tony Liu | last post by:
Hi, I want to get the name of the calling function of an executing function, I use the StackTrace class to do this and it seems working. However, does anyone think that there any side effect...
6
by: Jon Hyland | last post by:
Ok, I'm a little rusty on this, it should be a simple problem but I can't figure it out. How can I handle form events in my main code page?? I'm creating a Windows App in C#. Rather than make...
6
by: johnf401 | last post by:
I've got a VB .NET Web application that has several frames (for discussion sake, let's call them Form1.aspx and Form2.aspx). I want to be able to call a code module in Form2.aspx.vb from code...
3
by: Opa | last post by:
Hi , I have a form with javasript which launches a popup via the showModalDialog() method. I get the dialog to open, now I am trying to first get a reference to the calling form from the popup...
4
by: Bugs | last post by:
Hi, I wonder if anyone can help me out. I'm building a vb.net application that has a form with a panel that contains several other sub forms (as a collection of controls). What I'm wanting to...
7
by: Kenneth Brody | last post by:
The recent thread on "query about main()" got me thinking... As I recall, calling a function with the wrong parameters causes undefined behavior. (These all assume that no prototype of foo()...
47
by: mukeshrasm | last post by:
Hi I am calling two pages using Ajax Get_Pages.php and Get_Content.php from combo box. Both pages are displayed based on selection from combo box. Main problem is that it is not showing the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.