JavaScript How to overwrite the values of the first column in every rows in a HTML tbody table with JavaScript and with jQuery?

Here is a table example:

<table id="tableId">
 <thead>
  <tr>
   <th>line number</th>
   <th>value</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>2</td>
   <td>value 1</td>
  </tr>
  <tr>
   <td>3</td>
   <td>value 2</td>
  </tr>
  <tr>
   <td>1</td>
   <td>value 3</td>
  </tr>
 </tbody>
</table>
<input type="button" value="relineing" onclick="reLineNumbering('tableId')"/>

I want only the "line number"s to be in sequence like this:

<table id="tableId">
 <thead>
  <tr>
   <th>line number</th>
   <th>value</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>1</td>
   <td>value 1</td>
  </tr>
  <tr>
   <td>2</td>
   <td>value 2</td>
  </tr>
  <tr>
   <td>3</td>
   <td>value 3</td>
  </tr>
 </tbody>
</table>
<input type="button" value="relineing" onclick="reLineNumbering('tableId')"/>

I've tried both of the snippets below:

function reLineNumbering(tableId) {
  $('#'+tableId+' tbody').each(function (i) {
    this.rows[i].cells[0].text('i');
  });
}

function reLineNumbering(tableId) {
  var rowCount = $('#'+tableId+' tbody tr').length;
  for (var i=0; i<rowCount; i++) {
    $('#'+tableId+' tbody').rows[i].cells[0].text(i);
  }
}

Could someone help me?

Answer:1

This will change the first column to a sequential number starting from 1:

function reLineNumbering(tableId){
    $('#' + tableId + ' > tbody > tr').each(function(i, val){
        $('td:first', this).text(i+1); 
    });
}

Fiddle

Plain Javascript - Fiddle:

function reLineNumbering(tableId){
    var table = document.getElementById(tableId);
    var total = table.rows.length;
    for(var i=0; i<total; i++){
        if(i > 0){
            table.rows[i].cells[0].innerHTML = i;
        }
    }
}

Or by creating the text node instead of setting innerHTML. In this simple scenario the use of innerHTML isn't a problem, but usually you will want to work with DOM elements and set the text node instead of setting the HTML:

function reLineNumbering(tableId){
    var table = document.getElementById(tableId);
    var total = table.rows.length, text, cell;
    for(var i=0; i<total; i++){
        if(i > 0){
            text = document.createTextNode(i);
            cell = table.rows[i].cells[0];
            cell.removeChild(cell.firstChild);
            cell.appendChild(text);
        }
    }
}
Answer:2

Try

$('#tableId > tbody > tr').find('td:first').text(function(idx, text){
    return idx + 1
})

Demo: Fiddle

Answer:3

this is the correct answer:

function reLineNumbering(tableId) {
        var myTable=document.getElementById(tableId);
        var rowCount = myTable.rows.length;
        for (var i = 1; i < rowCount; i++) {
            myTable.rows[i].cells[0].innerHTML = i;
        }  
    }
Answer:4

How do you pass something to a function, as if it calls the function? Say I have this function function ShowId() { alert($(this).attr('id')); } and this HTML <div id='div1'> <a class='...

How do you pass something to a function, as if it calls the function? Say I have this function function ShowId() { alert($(this).attr('id')); } and this HTML <div id='div1'> <a class='...

  1. javascript pass jquery object as parameter
  2. javascript pass jquery object to function
  3. javascript function pass jquery
  4. jquery pass javascript variable to php

I am coding with processing.js. I want the size variable to get greater as the cursor (mouse) approches the ellipse and to get smaller as the cursor moves away from the ellipse. The size should (if ...

I am coding with processing.js. I want the size variable to get greater as the cursor (mouse) approches the ellipse and to get smaller as the cursor moves away from the ellipse. The size should (if ...

I have tried these to snippets of codes, the first one works in IE and Chrome, the second one works in Chrome only, but both of them don't work in Firefox. What I want is to stop the page from going ...

I have tried these to snippets of codes, the first one works in IE and Chrome, the second one works in Chrome only, but both of them don't work in Firefox. What I want is to stop the page from going ...

Does javascripts typeof expression check for null? var test = {}; console.log(typeof test['test']);//"undefined" var test = null; console.log(typeof test['test']);//TypeError: test is null ...

Does javascripts typeof expression check for null? var test = {}; console.log(typeof test['test']);//"undefined" var test = null; console.log(typeof test['test']);//TypeError: test is null ...

  1. does javascript have classes
  2. does javascript need semicolons
  3. does javascript pass by reference
  4. does javascript have pointers
  5. does javascript compile
  6. does javascript short circuit
  7. does javascript have garbage collection
  8. does javascript have inheritance
  9. does javascript use semicolons
  10. does javascript have interfaces
  11. does javascript have dictionaries
  12. does javascript have a garbage collector
  13. does javascript have enums
  14. does javascript have tuples
  15. does javascript follow pemdas
  16. does javascript have a main function
  17. does javascript have integers
  18. does javascript run on jvm
  19. does javascript have sets
  20. does javascript have hashmap