Get ID from element via tagname - Javascript DOM

Javascript examples for DOM:Element id

Description

Get ID from element via tagname

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <script>
function getElements(){/*  ww  w .  j a v a 2  s  . co m*/
  var x=document.getElementsByTagName("input");
  console.log(x[0].id);
}

      </script> 
   </head> 
   <body> 
      <input id="hi" type="text" size="20">
      <br> 
      <input type="text" size="20">
      <br> 
      <input type="text" size="20">
      <br>
      <br> 
      <input type="button" onclick="getElements()" value="What is the ID for the first element?">  
   </body>
</html>

Related Tutorials