javascript get every element with id code example

Example 1: javascript get all elements of an id

// You can't
// You can only have 1 instance of an id per document
// You need to use classes instead

var x = document.getElementsByClassName("example");

Example 2: javascript get multiple elements by id

<!-- html -->
<a class="test" name="Name 1"></a>
<a class="test" name="Name 2"></a>
<a class="test" name="Name 3"></a>

// javascript
var elements = document.getElementsByClassName("test");
var names = '';
for(var i=0; i<elements.length; i++) {
    names += elements[i].name;
}
document.write(names);