Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do I dynamically identify the forms on a document?

    Situation:

    A single page having two forms... form1 and form2
    A left nav of links.

    The links all call a javascript function called "triggerSubmit()".

    Based on the link selected, I need to save a unique value to a form.

    the function:
    function triggerSubmit(someValue) {
    document.formName.uniqueValue.value=someValue;
    document.formName.submit();
    }

    The link:
    <a href="javascript:triggerSubmit('gblNavigationAction');">Link Text</a>


    Because the left nav is included on many pages each having it's own form(s),
    How can I make the function triggerSubmit more dynamic to determine the form names on a page? -and/or- if no form exists how do I check that also with the mindset of being more dynamic and not hard coded... I don't wish to copy the function on every page and customize it.

    Any tips would be appreciated...
    Thanks upfront for your assist!
    -Ron

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Something on these lines maybe?


    PHP Code:
    function triggerSubmit(someValue) {

    numberOfForms=document.forms.length

    if(numberOfForms==0){

    alert("There are no forms")

    }
    else{

    for(var 
    i=0;i<numberOfForms;i++){

    if(
    document.forms[i].name == "yourFormName"){
    document.forms[i].uniqueValue.value=someValue;
    document.form[i].submit();
    }

    }

    }


    Last edited by Mr J; 07-12-2005 at 10:43 PM.
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #3
    New Coder
    Join Date
    Jul 2005
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    forms array

    Hi,

    The simple example I've written below should help you. The forms can be accessed through the javascript forms array. This array is accessed through the document object,

    document.forms.length // number of forms on page

    and to access the name of each form you use the name property of the form,

    document.forms[0].name // name of first form on page.

    The forms are numbered from 0 up to 1 less than the number of forms on the page.


    Example:

    <HTML>
    <HEAD>
    <SCRIPT>
    <!--
    var num_forms
    var form1
    var form2

    function countforms() {
    num_forms = document.forms.length;
    form1 = document.forms[0].name;
    form2 = document.forms[1].name;
    }

    //-->
    </SCRIPT>

    </HEAD>

    <BODY>
    <FORM NAME="form1">
    </FORM>

    <FORM NAME="form2">
    </FORM>

    <INPUT TYPE=BUTTON VALUE="count forms" ONCLICK="countforms();">
    </BODY>
    </HTML>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •