Set ID as incremental variable in HTML - Javascript DOM

Javascript examples for DOM:Element setAttribute

Description

Set ID as incremental variable in HTML

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){/*from  w  w w .  j  a  va2s  . c om*/
(function() {
    var x = document.getElementsByTagName('div');
    for(i=0; i<x.length; i++) {
        x[i].setAttribute('id', [i]);
        console.log(x[i]);
    }
    })();
    }

      </script> 
   </head> 
   <body> 
      <div></div> 
      <div></div>  
      <div id="hello"></div> 
      <div id="hi"></div> 
      <div></div> 
   </body>
</html>

Related Tutorials