-1

Here is the code I want to process using javascript / jquery

EDITED THE BELOW CODE BLOCK FOLLOWING THE COMMENTS

    <table>
       <tbody>
<tr class="promote">
           <td>1</td>
           <td><span class="team_logo visible-lg visible-sm" style=""></span><a class="team_name " href="/team/show/7473/Power-Hiters">Power Hiters</a><span class="online" title="user is online"></span></td>
           <td style="text-align:center">5</td>
           <td style="text-align:center">10</td>
           <td style="text-align:center">5</td>
           <td style="text-align:center">0</td>
           <td style="text-align:center">0</td>
           <td style="text-align:center">6.713</td>
        </tr>
<tr> <!-- Similar Above tr Content --> </tr>
          <tr> <!-- Similar Above tr Content --> </tr>
          <tr> <!-- Similar Above tr Content --> </tr>
       </tbody>
    </table>

I want to get the inner content/html of each <td> tag in a array using javascript / jquery


Extra Info:

When I tried it seems jquery strips the td and tr tags.

Actual thing I want to do is extract td in a multi dimensional array for each tr the table structure:

<table>
   <tbody>
      <tr> <!-- Above tr Content --> </tr>
      <tr> <!-- Above tr Content --> </tr>
      <tr> <!-- Above tr Content --> </tr>
   </tbody>
</table>
6
  • <div id="cache" style="display: none;"><tr class="promote"> Your HTML is invalid. tr must be child of table
    – Satpal
    May 19, 2014 at 10:03
  • 3
    The HTML is invalid. A tr can't be the direct child of a div.
    – JJJ
    May 19, 2014 at 10:03
  • what you have tried so far ? and your <table> TAG ? May 19, 2014 at 10:04
  • can the second code block be used for getting the td data May 19, 2014 at 10:04
  • hi, please check the code jsfiddle.net/F5PS5 . Is this you are looking ?.
    – Renjith
    May 19, 2014 at 10:11

2 Answers 2

2

Since you want a multidimensional array, you will need to:

  • Make an array that will hold all table data
  • Iterate through all tr and create an array per row
  • Push that array in the first one, that holds all table data.

something like this:

var tableContent = [];
$('table tr').each(function(){
  var trcontent = [];
  $('td', this).each(function(){ trcontent.push($(this).text()); });
  tableContent.push(trcontent);
})

console.log(tableContent);

example in jsfiddle

0

Try,

var arr = $("#cache").find('td').map(function(){ return this.textContent; }).get();
2
  • tr must be child of table
    – Satpal
    May 19, 2014 at 10:04
  • @Satpal Yeah.. thanks for the openness to downvote. I just read the mark up fully, kind of invalid one. May 19, 2014 at 10:05

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.