Skip to main content

Using JavaScript to access form objects when there are multiple forms

var oForm = document.getElementById('subscribe_frm');

OR

var oForm = document.forms[1];

OR

var oForm = document.forms["submit_bookmark"];

We can use the same approach to access individual form elements as well. Every form field of a form is contained in the form’s elements collection which is a JavaScript array similar to the document object’s forms collection.

To access the text input field named “tags_list” of the form named
“submit_bookmark” (which was the second form on the web page, as you saw
in the demo), we can use one of the following methods:

  • First, get the form reference:
  • var oForm = document.forms["submit_bookmark"];
  • Next, we get the reference to the “tags_list” element in one of the following ways:
  • var oTagsList = oForm.elements[2]; OR
    var oTagsList = oForm.elements["tags_list"];
  • To get the value of the input element, as before:
  • var tags = oTagsList.value;

We could implement this by passing the names of the two forms as well
as the names of the source and target elements to a javascript
function:

<a href="#" onClick=" javascript: copyFormElementToElementOfDifferentForm('submit_article', 'submit_bookmark', 'site_cat', 'tags_list');">Copy site_cat element value of the first form to the input field named "tags_list" of the second form</a>

Have a look at the implementation of this function:

var oForm1 = document.forms[oForm1Name];
var oForm2 = document.forms[oForm2Name];
var oForm1Element = oForm1[oForm1ElementName];
var oForm2Element = oForm2[oForm2ElementName];
if (oForm2Element.value == '')
{
    oForm2Element.value += oForm1Element.value;
}
else
{
    oForm2Element.value += ', ' + oForm1Element.value;

}

Example: Using Javascript to access a form when there are multiple forms

Write an Article (form name attribute="submit_article")

Bookmark A Page (form name attribute="submit_bookmark")

Getting a reference to the above forms and their elements

Demonstrating the use of document.forms[name]

Demonstrating the use of document.forms[index]

The code


function showFormElements(oForm) {

  var cnt = 0;
  var msg = "Form with 'name' attribute='" + oForm.name + "'";
  var str = "\nThe elements are: \n\n";
  for (i = 0; i < oForm.length; i++) {

  cnt ++;
  str += oForm.elements[i].tagName + " with 'name' attribute='" + oForm.elements[i].name + "'\n";
  }

  msg += " has " + cnt + " elements. \n" + str;

  alert(msg);
}

function showFormData(oForm) {
  var msg = "The data that you entered for the form with 'name' attribute='" + oForm.name + "': \n";
 

  for (i = 0; i < oForm.length, oForm.elements[i].getAttribute("type") !== 'button'; i++) {
    msg += oForm.elements[i].tagName + " with 'name' attribute='" + oForm.elements[i].name + "' and data: ";
    if(oForm.elements[i].value == null || oForm.elements[i].value == '') {
    msg += "NOT SET \n";

    } else {
      msg += oForm.elements[i].value + "\n";
    }
  }

  alert(msg);
}

function showElementsForTargetForm(targetForm, element_type) {
  var cnt = 0;

  var msg = "Form with 'name' attribute='" + targetForm.name + "'";
  var str = '\n';
  for (i = 0; i < targetForm.length; i++) {
  if(targetForm.elements[i].tagName == element_type) {  

    cnt ++;
    str += "\nThe " + element_type + " element with 'name' attribute='" + targetForm.elements[i].name + "'\n";
  }
  }

  msg += " has " + cnt + " " + element_type + " element(s)." + str;
  alert(msg);

}

function showElementsForTargetFormName(oForm) {
  var targetFormName = oForm.elements["form_name"].value;
  var element_type = oForm.elements["element_type"].value;
  var targetForm = document.forms[targetFormName];

  showElementsForTargetForm(targetForm, element_type);
}

function showElementsForTargetFormNumber(oForm) {
  var targetFormNumber = oForm.elements["form_number"].value;

  var element_type = oForm.elements["element_type"].value;
  var targetForm = document.forms[targetFormNumber];

  showElementsForTargetForm(targetForm, element_type);
}


Reference:

http://www.javascript-coder.com/javascript-form/javascript-get-all-form-...