Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9309513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:53:04+00:00 2026-06-19T00:53:04+00:00

The Javascript increments each visible number in these dynamically generated rows of a static

  • 0

The Javascript increments each visible number in these dynamically generated rows of a static table.

The jQuery dutifully hides each row when clicked, but, of course, the numbers don’t change. How can I use jQuery to re-number the rows when the visitor is done removing the unwanted lines? I want the user to click ‘renumber’ to renumber the remaining rows correctly. What I have didn’t work 🙂

Help is deeply appreciated.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
 <script language="javascript" type="text/javascript">
 var keepTrack = 1;      
 </script>
 <table>
 <tr><td colspan="2" id="renumber">renumber</td></tr>

 <tr id="sWH1">
 <td>
 <script>
 document.write(keepTrack);keepTrack++;
 </script></td>
 <td>someWordsHere</td></tr>

<script>
$(document).ready(function() { 
$("#sWH1").click(function() {
$("#sWH1").hide(100);}); 
 });</script>


 <tr id="sWH2">
 <td>
 <script>
 document.write(keepTrack);keepTrack++;
 </script></td>
 <td>someWordsHere</td></tr>

 <script>
 $(document).ready(function() { 
 $("#sWH2").click(function() {
 $("#sWH2").hide(100);}); 
  });</script>


 <tr id="sWH3">
 <td>
 <script>
 document.write(keepTrack);keepTrack++;
 </script></td>
 <td>someWordsHere</td></tr>

 <script>
 $(document).ready(function() { 
 $("#sWH3").click(function() {
 $("#sWH3").hide(100);}); 
  });</script>

 </table>

 <script language="JavaScript" type="text/JavaScript">
 $(document).ready(function() {
 $("#renumber").click(function() {
 'magically renumber the remaining table rows'
 });
  });
 </script>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T00:53:06+00:00Added an answer on June 19, 2026 at 12:53 am

    The straightforward answer

    To renumber the rows you can use the each() method that allows you to iterate over a set of elements (the rows in this case) and provides an i enumerator parameter.

    A general example:

    $(some-tr-selector).each(function(i) {
        $(this).children(some-td-selector).text(i+1);
    });
    

    Now to do this, you’ll need to define better selectors. The problem with your current code is that your selectors are too specific. This prevents you from creating general event handlers, instead of creating a click handler for each row.

    How to improve your code

    Consider setting a class for the relevant numbered rows. This will allow you to create a single click handler for all rows and make the relevant rows much easier to select when renumbering.

    For example:

    HTML

    <table>
        <tr><td colspan="2" id="renumber">renumber</td></tr>
        <tr class="numbered_row">
            <td class="row_number"></td>
            <td>someWordsHere</td>
        </tr>
        <tr class="numbered_row">
            <td class="row_number"></td>
            <td>someWordsHere</td>
        </tr>
        <tr class="numbered_row">
            <td class="row_number"></td>
            <td>someWordsHere</td>
        </tr>
        ...
    </table>
    

    Javascript

    $(document).ready(function() {
         // Number all rows onload
        $('table tr.numbered_row').each(function(i) {
            $(this).children('.row_number').text(i+1);
        });
    
         // Row click handler
        $('table tr.numbered_row').on('click', function() {
            $(this).hide(100);
        });
    
         // Renumber click handler
        $('#renumber').on('click', function() {
            $('table tr.numbered_row:visible').each(function(i) { // Iterate over all _visible_ rows
                $(this).children('.row_number').text(i+1);
            });
        });
    });
    

    Note how by adding two classes numbered_row and row_number we are able to do everything efficiently in one block of code: initial numbering of the rows, a single click handler to hide them and the renumbering action.

    An even more general approach

    If you wanted, you could make this even more general and efficient, by chaining the each() and on() methods as well as handling the renumbering within the on() each time a row is hidden.

    For example:

    $(document).ready(function() {
         // Number all rows onload
        $('table tr.numbered_row').each(function(i) {
            $(this).children('.row_number').text(i+1);
         // Row click handler
        }).on('click', function() {
             // Hide current row
            $(this).hide(100);
             // Renumber visible rows
            $('table tr.numbered_row:visible').each(function(i) { // Iterate over all _visible_ rows
                $(this).children('.row_number').text(i+1);
            });
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

JavaScript/JQuery var arr=[]; $('.element').each(function(i) { arr.push({id:i,value:$(this).attr('data-value')}); }); $.get('/json.php?arr='+arr,function(result) { alert(result); }); PHP <?php $j
I want increment #hidtr value in jquery. <script type=text/javascript> $('#hidtr').val(parseInt($('#hidtr').val()+1)); alert($('#hidtr').val()); </script>
JavaScript: $('.addTable').live('click', function () { var appendTxt = <table id='tab1'> <tr><td><input type='text' name='input1' /></td><td><input
Javascript's getTime() method returns the number of milliseconds since midnight of January 1, 1970.
Javascript can manipulate the document the browser is displaying, so the following: <script> document.write(<table><tr><td>Hola</td><td>Adios</td></tr></table>);
I am trying to increment a number by a given value each second and
I have an sql table named clients where each client is stored in a
I'm trying to have plain (no jQuery, mootools, etc) JavaScript add span tags with
For practice I am trying to display a number that increments from 0 -
Please no jQuery code, as I want to learn javascript first and foremost. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.