Solved

How do I iterate through cells of data in an html table using Java Script.

Posted on 2014-07-23
3
171 Views
Last Modified: 2014-07-28
Hello, I know how to iterate through rows of data in an html table using Java Script. However I need to learn how to iterate through each data cell in the row. Meaning data surrounded by <td></td> tags. Can someone show me how to do this? I have the code below which iterates through rows.

var iterate = 0;

while (rowser = table.rows[iterate])
{
  iterate++;
}
0
Comment
Question by:brgdotnet
3 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 250 total points
ID: 40216469
Just like a table has a property rows which holds an array of row objects, so each row object has a property cells which holds an array of cell objects. In keeping with your example you could iterate through the cells of each row like this:
var iterate = 0, rowser, celliterate, cellser, table = document.getElementById('table');

while (rowser = table.rows[iterate])
{
  celliterate = 0;
  while (cellser = rowser.cells[celliterate])
  {
    //do something with cellser for example:
    //console.log('row # ' + iterate + ', cell # ' + celliterate + ', contents: ' + cellser.innerHTML);
    celliterate++;
  }
  iterate++;
}

Open in new window

0
 
LVL 41

Assisted Solution

by:guru_sami
guru_sami earned 50 total points
ID: 40217491
You can use jquery like shown below: Sample on JsBin.

<html>
<head>
<script src="//code.jquery.com/jquery-1.9.1.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
  <script>
      $(function () {
            $("#clickme").click(function () {
                $("#table1 tr td").each(function (index) {
                    alert("td" + index + "="+$(this).html());
                });
            });
        });
  </script>
</head>
<body>
  <span id="clickme" style="color:blue;">Click Me</span>
<table id="table1">
            <tr>
                <td>One</td>
            </tr>
            <tr>
                <td>Two</td>
            </tr>
            <tr>
                <td>Three</td>
            </tr>
            <tr>
                <td>Four</td>
            </tr>
        </table>
</body>
</html>

Open in new window

0
 
LVL 2

Author Closing Comment

by:brgdotnet
ID: 40225919
Thanks, I needed a Java Script solution.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question