473,812 Members | 2,795 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling a function from 2 different forms on different pages

I have this function below

function checkLength(fie ld, 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="addComp uter.php"
ONSUBMIT="retur n ValidateForm(th is)">
Hardware Ethernet
<input type="text" name="hardwareE therneta" maxlength=2 size=1
ONKEYUP="checkL ength('1','2')" >
</form>

My problem is that I have another form on a different page:
<form name="form2" method="POST" action="addComp uter.php"
ONSUBMIT="retur n ValidateForm(th is)">
Hardware Ethernet
<input type="text" name="hardwareE therneta" maxlength=2 size=1
ONKEYUP="checkL ength('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 1639
Bryan wrote:
I have this function below

function checkLength(fie ld, 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="addComp uter.php"
ONSUBMIT="retur n ValidateForm(th is)">
Hardware Ethernet
<input type="text" name="hardwareE therneta" maxlength=2 size=1
ONKEYUP="checkL ength('1','2')" >
</form>

My problem is that I have another form on a different page:
<form name="form2" method="POST" action="addComp uter.php"
ONSUBMIT="retur n ValidateForm(th is)">
Hardware Ethernet
<input type="text" name="hardwareE therneta" maxlength=2 size=1
ONKEYUP="checkL ength('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="checkL ength('form1',' thisFieldName', 'nextFieldToFoc us')"

function checkLength(for mName,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="checkL ength(this.form ,this,'nextFiel dName')"

I will leave it to you to try to code the second one.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript 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
12706
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 state, the HTML page URL would change from default.html to default.html#stateA. example: http://pearstudios.com/javascript/locationHashAndFlash.html This has worked perfectly within HTML and javascript alone, however, when connecting Flash and...
10
600
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 called MyFunction. Can you specify a string for the function name like you can with the form name? That is can I do something like Forms("MyForm")."MyFunction"
2
9786
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 towards this approach such as how would it works in multi-thread. Thanks Tony
6
17581
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 my main form the startup object, I'd rather put my Main() function in a class or code file. Why? Because thats what I always do in standard C++ or VB. In my opinion, writing all my application logic in the form itself is sloppy and only for...
6
4679
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 module in Form1.aspx.vb. How do I format this statement? I'm not interested in calling client-side script on Form2.aspx from Form1.aspx.vb - I want to fire off server-side module in Form2.aspx.vb
3
3585
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 and then do a refresh of the calling form. Any ideas on how to get a reference to the calling form? I tried window.parent.location.reload() , but it doesn't work. Thanks a lot.
4
2257
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 do is call a generically named public sub in the top-most sub form in the panel. I have the name of the top-most form as a string (eg. g_TopForm = "frmCustomers") and I can reference the form with:
7
1910
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() is in scope.) For example: ==== fileA.c
47
3305
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 editor which is called on the Get_Content.php. in Main Page. I have included the js files in the head section. all the files are on the same directory.
0
9734
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9607
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10663
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10401
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9217
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5704
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4357
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3029
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.