html - How to automaticaly add an incrementing numeric id for each td elements in javascript -


i want javascript automatically add incrementing numeric id each of td elements.

i want script @ bottom of each html page tell first id input page , ++ automaticaly each next td element. tried lot of things loop , missing appendchild method, can't make work know i'm there.

if give hand, appreciated!

here how manually enter ids each new month :

<tr>     <td id="603" class="offweekend"></td>     <td id="604" class="offmonth"></td>     <td id="605" class="offmonth"></td>     <td id="606" class="offmonth"></td>     <td id="607" class="offmonth"></td>     <td id="608" class="offmonth"></td>     <td id="609" class="weekend">1</td> </tr>  <script>        tds = document.getelementsbytagname("td");         tdlength = tds.length;         firsttd = 603;         lasttd = firsttd + tdlength;         (i = firsttd; < lasttd; i++){             td.appendchild() //???that's i'm confused, i'm wrong                                    approach?         } </script>     //thank you, i'm still learning :) 

assuming have variable firstvalue stores id of first td should be, can use this:

document.queryselectorall("td").foreach((v,i)=>v.id=i+firstvalue); 
  • the queryselectorall grabs of td elements in order nodelist.
  • v td element , i position of element in array

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -