0

I have the html table structured in this way:

<table class="generaltable boxaligncenter" id="outlinetable" cellpadding="1">
  <thead>
    <tr>
      <th class="header c0">Activity</th>
    </tr>
  </thead>
  <tbody id="yui_3_17_2_1_1599471686149_651">
    <tr class="">
      <td class="activity cell c0">
        <a class="dimmed" href="http://somesite.com.np/mod/questionnaire/view.php?id=304">Survey on student familiarity with the online learning
              platform</a
            >
          </td>
        </tr>
        <tr class="">
          <td class="activity cell c0">
            <a
              href="http://somesite.com.np/mod/assign/view.php?id=307"
              id="yui_3_17_2_1_1599471686149_724"
              >update your profile</a
            >
          </td>
        </tr>
        <tr class="">
          <td class="activity cell c0">
            <a href="http://somesite.com.np/mod/resource/view.php?id=352"
              >COURSE MANUAL</a
            >
          </td>
        </tr>
        <tr class="section" id="yui_3_17_2_1_1599471686149_685">
          <td
            class="cell c0 lastcol"
            colspan="4"
            id="yui_3_17_2_1_1599471686149_684"
          >
            <h3 id="yui_3_17_2_1_1599471686149_687">Week 9</h3>
          </td>
        </tr>
        <tr class="" id="yui_3_17_2_1_1599471686149_680">
          <td class="activity cell c0" id="yui_3_17_2_1_1599471686149_679">
            <a
              href="http://somesite.com.np/mod/page/view.php?id=568"
              id="yui_3_17_2_1_1599471686149_682"
              >Click me to get the welcome badge for week 9.</a
            >
          </td>
        </tr>
        <tr class="section" id="yui_3_17_2_1_1599471686149_698">
          <td
            class="cell c0 lastcol"
            colspan="4"
            id="yui_3_17_2_1_1599471686149_697"
          >
            <h3 id="yui_3_17_2_1_1599471686149_706">WEEK 9: ACTIVITIES</h3>
          </td>
        </tr>
        <tr class="" id="yui_3_17_2_1_1599471686149_702">
          <td class="activity cell c0" id="yui_3_17_2_1_1599471686149_701">
            <a
              href="http://somesite.com.np/mod/url/view.php?id=540"
              id="yui_3_17_2_1_1599471686149_700"
              >Keyboards for Disabled People</a
            >
          </td>
        </tr>
        <tr class="" id="yui_3_17_2_1_1599471686149_657">
          <td class="activity cell c0" id="yui_3_17_2_1_1599471686149_656">
            <a
              href="http://somesite.com.np/mod/url/view.php?id=541"
              id="yui_3_17_2_1_1599471686149_659"
              >Mobile interaction</a
            >
          </td>
        </tr>
        <tr class="section">
          <td class="cell c0 lastcol" colspan="4">
            <h3>Week 8</h3>
          </td>
        </tr>
        <tr class="">
          <td class="activity cell c0">
            <a href="http://somesite.com.np/mod/page/view.php?id=531"
              >Click me to get the welcome badge for week 8.</a
            >
          </td>
        </tr>
        <tr class="section">
          <td class="cell c0 lastcol" colspan="4">
            <h3>Week 8: Activities</h3>
          </td>
        </tr>
        <tr class="">
          <td class="activity cell c0">
            <a href="http://somesite.com.np/mod/hvp/view.php?id=519"
              >You Suck at Form Design ((Probably))</a
            >
          </td>
        </tr>
      </tbody>
    </table>

I want to select the href and the text inside td with the class="activity cell c0" and use the class text inside h3tag which is inside the class="cell c0 lastcol"

I am able to get the href and section inside a loop, but how to push that inside an array?

I want to push the value inside the newList.

var t2 = document.querySelectorAll('.generaltable.boxaligncenter tbody tr');
var newList = [];
var section = null;
for (var i = 0; i < t2.length; i++) {
  var href = t2[i].querySelector('td.c0 > a');  
  if(href===null){
   section = t2[i].querySelector('td.c0 > h3').innerText;
   console.log(`section ${section}`);
  }
  
  newList.push( {
        Activity: href.innerText,
        URL: href.getAttribute('href'),
        id:  href.getAttribute('href').substring(href.getAttribute('href').lastIndexOf('=') + 1),
        Section: section
  });
}

I have tried to push that to an array but there is the error: Uncaught TypeError: Cannot read property 'innerText' of null

Rasik
  • 1,961
  • 3
  • 35
  • 72

0 Answers0