What is getelementsbyname in javascript?

getElementsByName

The document.getElementsByName[name] is a method of the document object which is used to get an element by its name.

Syntax:

document.getElementsByName[name]

Example:

DOCTYPE html>


 
  
function fruitname[]  
{  
var allfruits =document.getElementsByName["fruit"];  
alert["Total Fruits:"+allfruits.length];  
}  
  
 
  
BANANA:  
APPLE:  
MANGO:  
STRAWBERRY:  
 
  
  
 

Document Object

Example

Get all elements with the specified name:

var x = document.getElementsByName["fname"];

Try it yourself »

More "Try it Yourself" examples below.

Definition and Usage

The getElementsByName[] method returns a collection of all elements in the document with the specified name [the value of the name attribute], as a NodeList object.

The NodeList object represents a collection of nodes. The nodes can be accessed by index numbers. The index starts at 0.

Tip: You can use the length property of the NodeList object to determine the number of elements with the specified name, then you can loop through all elements and extract the info you want.

Note: In HTML5, the "name" attribute is deprecated and has been replaced by the "id" attribute for many elements. Use the document.getElementById[] method where it is appropriate. Also look at the getElementsByClassName[] and getElementsByTagName[] methods.

Browser Support

Method
getElementsByName[] Yes Yes Yes Yes Yes

Syntax

document.getElementsByName[name]

Parameter Values

ParameterTypeDescription
name String Required. The name attribute value of the element you want to access/manipulate

Technical Details

DOM Version:Return Value:
Core Level 1 Document Object
A NodeList object, representing a collection of elements with the specified name. The elements in the returned collection are sorted as they appear in the source code.

More Examples

Example

Find out how many elements there are in the document that have a name attribute with the value "animal" [using the length property of the NodeList object]:

var x = document.getElementsByName["animal"].length;

Try it yourself »


Example

Check all elements with type="checkbox" in the document that have a name attribute with the value "animal":

var x = document.getElementsByName["animal"];
var i;
for [i = 0; i < x.length; i++] {
    if [x[i].type == "checkbox"] {
        x[i].checked = true;
    }
}

Try it yourself »

Document Object

What is difference between getElementById and getElementsByName?

The difference is that GetElementByID[] will retrieve a single element object based on the unique id specified, whereas GetElementsByTagName[] will retrieve an array of all element objects with the specified tag name. Then you can obtain the value using GetElementById from your C++ code.

What does getElementsByName return?

The getElementsByName[] method returns a collection of elements with a specified name. The getElementsByName[] method returns a live NodeList.

What is the use of getElementsByTagName in JavaScript?

The getElementsByTagName[] method returns a collection of all elements with a specified tag name. The getElementsByTagName[] method returns an HTMLCollection. The getElementsByTagName[] property is read-only.

What is the syntax of getElementsByTagName []?

Syntax: var elements = document. getElementsByTagName[name];

Bài mới nhất

Chủ Đề