Reputation: 2879
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
Upvotes: 0
Views: 81
Reputation: 34170
$('td.pos').each(function(){
$(this).html($(this).parent().index()+1);
});
Here is the DEMO
Upvotes: 1