Javascript document getElementsByName Method

Updated on 23 Mar 2012,
Published on 30 Apr 2009

Javascript DOM provides a getElementsByName method that can be accessed using the document object. getElementsByName method returns the array collection of HTML document elements present in the HTML web page with specified name. You can pass the value of name attribute of the HTML tags whose list of array you want to collect. In HTML, form objects that is objects associated with input tag support name attribute. You can specify the same name for different set of form objects for example input type text objects can be defined with same name. Javascript for loop can be used to get the value stored as HTML element at each index of the array returned by the getElementsByName method of Javascript. You can use the length property and item and tags functions of getElementsByName method of document object.

JavaScript DOM Examples:

You can see the live samples and examples of JavaScript DOM from the following links:

Syntax for Javascript getElemenstByName Method

var arr = new Array();
arr = document.getElementsByName( "Name" );

Above syntax shows how to use the getElementsByName method to generate the collection of HTML elements with specified name.

Properties and Functions of getElementsByName Method

You can access the following functions and properties of getElementsByTagName method:

1. item

item function accepts a parameter as index of the item you want to get from the array of specified HTML elements.

document.getElementsByName( "textfield" ).item(0)

above code will return the first element from the array collection having name attribute value "textfield".

2. tags

tags function of getElementsByName method also accepts one parameter as name of the HTML element e.g.: input or img. It returns only the specified HTML element from the array collection.

document.getElementsByName( "textfield" ).tags("img")

This function is the clone of Javascript getElementsByTagName method.

3. length

length property returns the number of HTML elements with specified name retrieved from the document.

document.getElementsByName( "textfield" ).length

In the next tutorial we will learn the Javascript getElementsByName method with example. We will run javascript for loop over the array collection of HTML elements generated by using getElementsByName method.

1 Responses to "Javascript document getElementsByName Method"
??: document.getElementsByName("divName").tags is not a function

document.getElementsByName("ElementName").tags is not support in FireFox and not support in IE,is not a function
Leave a Comment
* required
* required
* will not be published
* optional
* hint:
  • Subscribe via Email