How to get HTML elements by name attribute in Javascript

Get elements by name

getElementsByName(nameOfNameAttribute) gets the elements by name and returns HTMLElement[].

Example


<!DOCTYPE HTML> 
<html> 
    <body> 
        <p name="apple"> apple</p>
        <script> 
            var nameElems = document.getElementsByName("apple"); 
            document.writeln(nameElems.length); 
        </script> 
    </body> 
</html><!--from   ww  w .  jav  a  2  s .  c o m-->

Click to view the demo





















Home »
  Javascript »
    Javascript Reference »




Array
Canvas Context
CSSStyleDeclaration
CSSStyleSheet
Date
Document
Event
Global
History
HTMLElement
Input Element
Location
Math
Number
String
Window