Omari Victor Omosa
Omari Victor Omosa

Reputation: 2879

How to indicate position of a <tr> table row in a dynamic table list

I would want to show the index of my table list without the on click that are in most examples here in SO and other sources.

<script src="../js/jquery-2.0.2.min.js"></script>
<table id="tableID" width="200" border="1">
  <tr>
    <td class="sm">5</td>
    <td class="pos"></td>
  </tr>     
  <tr>
    <td class="sm">4</td>
    <td class="pos"></td>
  </tr>   
  <tr>
    <td class="sm">5</td>
    <td class="pos"></td>
  </tr>
</table>
<script>
var position  = getElementsByClassName("pos").index();;
document.getElementsByClassName("pos")[0].textContent = position;
</script>

I would want the above table to give a result like

enter image description here

Upvotes: 0

Views: 81

Answers (1)

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34170

$('td.pos').each(function(){
$(this).html($(this).parent().index()+1);
});

Here is the DEMO

Upvotes: 1

Related Questions