locked
how to repeat for loop RRS feed

  • Question

  • User-1634604574 posted

    i have this table i want to read this table row by for loop when loop completed read all rows of table remove first row then when repeate loop read the table row from 

    the first column after completed then remove first row an so on when table row are except th are zero then stop for loop how can i do it?

    (Note: my table row are dynamic it mean row of rows are dynamic not static loop depending on length of table)

    <table id="tb">

    <tr>

    <th>name</th>

    </tr>

    <tr><td>lam</td></tr>

    <tr><td>jds</td></tr>

    <tr><td>sk</td></tr>

    <tr><td>in</td></tr>

    </table>

    Saturday, March 9, 2019 7:26 AM

All replies

  • User475983607 posted

    Your requirement does not make logical sense.   When asking questions on the forum, always share the code that you wrote to solve the problem.  Explain the expected results and the actual results.  This helps us to help you because this process highlights holes in your understanding.  Then we can fill those holes with knowledge.  When you do not post code it looks like you are not trying.  

    The code sample below uses a button click to traverse table rows from top to bottom.   Feel free to update the code to suite your needs.

    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
        <style>
            .selected {
                background:gainsboro;
            }
        </style>
    
    </head>
    <body>
        <div>
            <form id="List_Form">
                <table>
                    <thead>
                        <tr>
                            <th>Id</th>
                            <th>First</th>
                            <th>Last</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr class="selected">
                            <td>1</td>
                            <td>Hello</td>
                            <td>World</td>
                        </tr>
                        <tr>
                            <td>2</td>
                            <td>Foo</td>
                            <td>Bar</td>
                        </tr>
                        <tr>
                            <td>3</td>
                            <td>Peter</td>
                            <td>Cong</td>
                        </tr>
                    </tbody>
                </table>
                <input id="Next" type="button" value="Next" />
            </form>
        </div>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script>
    
            $('#Next').click(function () {
                var row = $('.selected');
                var nextRow = $(row).next('tr').addClass('selected');
                if (!$(nextRow).is('tr')) {
                    $('#List_Form > table > tbody > tr:nth-child(1)').addClass('selected');
                }
                row.removeClass('selected'); 
            });
    
        </script>
    </body>
    </html>

    Saturday, March 9, 2019 1:12 PM
  • User-2054057000 posted

    Are you trying to read value by looping through all tr elements ?

    If yes then you can do it through jQuery Each method quite easily. The code looks something like this:

    $("table tr").each(function (index, value) { 
      console.log(value); // gives the trs value one by one
    }); 

    Now If you want to read all tds value then you extend the above code:

    $("table tr").each(function (index, value) { 
      $(value).find("td").each(function (index, value) { 
        console.log(value); // gives the tds value one by one 
      });  
    }); 

    Regards.

    Sunday, March 10, 2019 3:51 PM