get all html elements javascript code example

Example 1: select all checkboxes html js

<script language="JavaScript">
function toggle(source) {
  checkboxes = document.getElementsByName('foo');
  for(var i=0, n=checkboxes.length;i<n;i++) {
    checkboxes[i].checked = source.checked;
  }
}
</script>

<input type="checkbox" onClick="toggle(this)" /> Toggle All<br/>

<input type="checkbox" name="foo" value="bar1"> Bar 1<br/>
<input type="checkbox" name="foo" value="bar2"> Bar 2<br/>
<input type="checkbox" name="foo" value="bar3"> Bar 3<br/>
<input type="checkbox" name="foo" value="bar4"> Bar 4<br/>

Example 2: select all elements javascript

var elements = document.getElementsByTagName("*");

Example 3: 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");

Tags:

Java Example