Wednesday, June 8, 2016

JavaScript getting elements by name

The id of a HTML element should be unique. But the names of couple of elements can be same for our convenience.

Unlike getElementById, which returns a single element,

var elementID = document.getElementById("id123");

getElementsByName returns an array of all the elements which are having the name.

var elementArr = document. getElementsByName("myElement");

We can loop through this array If we need to perform same operation on all the elements like changing the style.