Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 516
  • Last Modified:

Traversing through HTML Table rows by using javascript

Hi,

I have a table created in HTML like this
<table id="table1">
    <tr>
        <td>
              "text"
        </td>
         <td>
              "text 2"
        </td>
    </tr>
     <tr>
        <td>
              "text"
        </td>
         <td>
              "text 2"
        </td>
    </tr>
</table>
Now i want to traverse through each and every row and just get the data which is there in the first column i.e, TD in each row.
How can i achieve that by using JavaScript..

Any help is appreciated
0
Needful
Asked:
Needful
2 Solutions
 
hieloCommented:
try:
var d = getData("table1");
alert( d );
function getData(tableId){
 var t = document.getElementById(tableId);
 var data="";
 for( var i=0, limit=t.rows.length; i < limit; ++i)
 {
   data += ";"+t.rows[i].cells[0].innerHTML;
 }
return data.substring(1);
}

Open in new window

0
 
cstsangCommented:
is it what you need?
<table id="table1">
    <tr>
        <td>
              "data1"
        </td>
         <td>
              "text 2"
        </td>
    </tr>
     <tr>
        <td>
              "data2"
        </td>
         <td>
              "text 2"
        </td>
    </tr>
</table>
<script language=javascript>
var d = getData("table1");
alert( d );
function getData(tableId)
{
 var t = document.getElementById(tableId);
 var data="";
 for( var i=0, limit=t.rows.length; i < limit; ++i)
 {
   data += "row "+i+",cell 1="+t.rows[i].cells[0].innerHTML+"\n";
 }
 return data;
}
</script>

Open in new window

0
 
Michel PlungjanIT ExpertCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now