Calling a function from 2 different forms on different pages

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bryan

    #1

    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!!!!
  • Randy Webb

    #2
    Re: Calling a function from 2 different forms on different pages

    Bryan wrote:
    [color=blue]
    > 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!!!![/color]

    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/

    Comment

    Working...