X
HOME HTML CSS JAVASCRIPT SQL PHP JQUERY ANGULAR BOOTSTRAP XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

HTML DOM getElementsByName() Method

Document Object Reference 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

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

Technical Details

DOM Version: Core Level 1 Document Object
Return Value: 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.


Examples

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 (<input 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++) {
    x[i].checked = true;
}

Try it yourself »


Document Object Reference Document Object

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB HOSTING

UK Reseller Hosting

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates

SHARE THIS PAGE

facebook