Get HTML elements by name attribute in JavaScript

Description

The following code shows how to get HTML elements by name attribute.

Example


<!--from  w  ww.  ja  v a 2  s.  c  om-->

<!DOCTYPE HTML>
<html>
<body>
<p name="apple"> apple</p>
<script>
var nameElems = document.getElementsByName("apple");
document.writeln(nameElems.length);
</script>
</body>
</html>

Click to view the demo

The code above generates the following result.

Get HTML elements by name attribute in JavaScript