Javascript examples for DOM:Element setAttribute
Set ID as incremental variable in HTML
<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>