HTML DOM getElementsByName() Method

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP







<!--
main_leaderboard, all: [728,90][970,90][320,50][468,60]
-->



HTML DOM getElementsByName() Method


❮ 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








ParameterType Description
nameStringRequired. The name attribute value of the element you want to access/manipulate




<!--
mid_content, all: [300,250][336,280][728,90][970,250][970,90][320,50][468,60]
-->




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.

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 <input> 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

Popular posts from this blog

Malaysia to Papua New Guinea by motorcycle?

List of Bhojpuri films

PHP code is not being executed, instead code shows on the page