4

I have a table in HTML that has 5 columns. The first column is the "row number", where I want to show which row it is--starting from 1.

Here's a picture

I have tried using this CSS:

body {
    /* Set the Serial counter to 0 */
    counter-reset: Serial; 
}

table {
    border-collapse: separate;
}

tr td:first-child:before {
    /* Increment the Serial counter */
    counter-increment: Serial;

    /* Display the counter */
    content: "Serial is: " counter(Serial); 
}
3
  • 1
    show us what you have tried Commented Aug 20, 2016 at 18:53
  • body { counter-reset: Serial; /* Set the Serial counter to 0 / } table { border-collapse: separate; } tr td:first-child:before { counter-increment: Serial; / Increment the Serial counter / content: "Serial is: " counter(Serial); / Display the counter */ } /*This is the code that i have tried*/ CSS code Commented Aug 20, 2016 at 18:57
  • 1
    edit your post with this code plz Commented Aug 20, 2016 at 19:03

3 Answers 3

9

You can use next option which is through css again: Note: class="css-serial"

<table class="css-serial">
  <thead>
    <tr>
      <th>#</th>
      <th>1st Column</th>
      <th>2nd Column</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td></td>           <!--intentionally left blank-->
      <td>Column 1</td>
      <td>Column 2</td>
    </tr>
    <tr>
      <td></td>           <!--intentionally left blank-->
      <td>Column 1</td>
      <td>Column 2</td>
    </tr>
    <tr>
      <td></td>           <!--intentionally left blank-->
      <td>Column 1</td>
      <td>Column 2</td>
    </tr>
  </tbody>
</table>

And add next style:

 <style>
/*adding row numbers through css*/
.css-serial {
    counter-reset: serial-number; /* Set the serial number counter to 0 */
}

    .css-serial td:first-child:before {
        counter-increment: serial-number; /* Increment the serial number counter */
        content: counter(serial-number); /* Display the counter */
    }

</style>
Sign up to request clarification or add additional context in comments.

Comments

1

Here is the working code for this:

<html>
    <head>
        <script type="text/javascript">
        function displayResult()
        {
            var index = document.getElementById("myTable").rows.length;
            var new_row = '<td>'+index+'</td><td>cell 1</td><td>cell 2</td>';
            document.getElementById("myTable").insertRow(-1).innerHTML = new_row;
        }
        </script>
    </head>

    <body>       
        <table id="myTable" border="1">
            <tr>
            `   <td>0</td>
                <td>cell 1</td>
                <td>cell 2</td>
            </tr>

        </table>
        <br />
        <button type="button" onclick="displayResult()">Insert new row</button>            
    </body>
</html>

1 Comment

If you add an explanation as to how this code works (I am especially puzzled by why the cell 1, cell 2 part is included in the script), this answer would be more helpful.
0

Without the code of how you're doing it it's hard to say. I'm assuming the rows are in a collection, since you have an add-function. Can't you just use the index + 1?

If the add functions just adds the raw html, you can get the table element and count the children (or use the last child) and calculate your number from that.

With the little info you gave that's all I can say.

2 Comments

How can I do "you can get the table element and count the children (or use the last child) and calculate your number from that"?
In your Fiddle, you have the table headers <th> inside a table row <tr>, which isn't right as far as I know. If you correct that, you can get the amount of rows in the table with this code: var rows = document.getElementById(myTable).getElementsByTagName("tr").length;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.