-1

When using the DOM to edit a table, how do you edit the contents of a specified cell?
I tried looking up different ways on w3Schools however console keeps giving me this message stating the values are null. Can somebody help?

This is the HTML for the table I am trying to edit:

<div id="courseSummaryContainer" class="tab">
    <table cellspacing="0" class="summaryTable courseSummary smallFontTable" summary="Health Care by province">
        <thead><tr>
            <th>Name</th>
            <th>Description</th>
            <th>Date of entry</th>
            <th>Rating</th>
            <th>Grade</th>
            <th>Submission entry type</th>
        </tr></thead>
        <tbody>
        <tr class="row-even">
            <td>Illinois</td>
            <td>Online system</td>
            <td>201602</td>
            <td>0100</td>
            <td>5</td>
            <td>Final</td>
        </tr>
        <tr class="row-odd">
            <td>Alabama</td>
            <td>Regional area health</td>
            <td>201606</td>
            <td>0100</td>
            <td>7</td>
            <td>Final</td>
        </tr>
        </tbody>
    </table>
</div>

I tried:

document.getElementById("table.summaryTable.courseSummary.sm‌​allFontTable").rows[‌​0].cells; x[0].innerHTML = "NEW CONTENT";

but it didn't work.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • Add javascript what you tried? – Mani Nov 28 '16 at 03:31
  • 1
    document.getElementById("table.summaryTable.courseSummary.smallFontTable").rows[0].cells; x[0].innerHTML = "NEW CONTENT"; – marshmellooooooos Nov 28 '16 at 03:34
  • 1
    That's what I used – marshmellooooooos Nov 28 '16 at 03:34
  • 1
    Based on your other questions, this is for a userscript, right? – Brock Adams Nov 28 '16 at 22:25
  • I am trying to create a user script out of this. – marshmellooooooos Dec 23 '16 at 23:53
  • @BrockAdams Thanks a lot for your help! I got my script working, however, have been having problems with tampermonkey scripts some reason. Can you please help me out? http://stackoverflow.com/questions/41338130/tampermonkey-script-does-not-run-on-another-computer – marshmellooooooos Dec 27 '16 at 18:03
  • 1
    Marshmellooooooos, that's not how Stack Overflow works. We all answer only when the mood strikes us (among other factors). You need to vote more and accept answers on more of your previous questions. Honest votes and answer-accepts are the bedrock of how Stack Exchange sites work to determine content quality. You need to chip-in more. – Brock Adams Dec 27 '16 at 19:13

2 Answers2

0

Use id for table and try

    <table cellspacing="0" id="courseSummaryContainer" class="summaryTable courseSummary smallFontTable" summary="Health Care by province">
        <thead>
            <tr>
                <th>Name</th>
                <th>Description</th>
                <th>Date of entry</th>
                <th>Rating</th>
                <th>Grade</th>
                <th>Submission entry type</th>
            </tr>
        </thead>
        <tbody>
            <tr class="row-even">
                <td>Illinois</td>
                <td>Online system</td>
                <td>201602</td>
                <td>0100</td>
                <td>5</td>
                <td>Final</td>
            </tr>
            <tr class="row-odd">
                <td>Alabama</td>
                <td>Regional area health</td>
                <td>201606</td>
                <td>0100</td>
                <td>7</td>
                <td>Final</td>
                </tr>
           </tbody>
    </table>


<script>

var x = document.getElementById("courseSummaryContainer").rows[0].cells;
x[0].innerHTML = "NEW CONTENT";

</script>
Mani
  • 2,675
  • 2
  • 20
  • 42
  • It is giving me the following error "VM52:1 Uncaught TypeError: Cannot read property '0' of undefined" – marshmellooooooos Dec 23 '16 at 23:55
  • If you dont have a row in table it will show the error. If table has atleast a row it will work. In the above just Run and check – Mani Dec 24 '16 at 03:07
  • This won't work for the OP because he can't pre-alter the HTML like that. He has no control over the page as served to his browser; this is a *userscripts* question. – Brock Adams Dec 27 '16 at 19:18
0

table.summaryTable.courseSummary.sm‌​allFontTable is not an ID for your table, so getElementById will not work.

That table has no ID, but it does have 3 CSS classes. Here's a tutorial on the difference.
See, also, the CSS Selectors reference at MDN.

So you can't use getElementById, but you can use document.querySelectorDoc.

Code as in the snippet below selects the first table, with the class courseSummary, that's contained by the courseSummaryContainer div.

Finally, avoid using innerHTML. Bad things come to those who do.

var csTable = document.querySelector ("#courseSummaryContainer table.courseSummary");
if (csTable) {
    csTable.rows[0].cells[0].textContent = "NEW CONTENT";
}
td, th {
    border: 1px solid gray;
    padding: 0.2ex 1ex;
}
table { border-collapse: collapse;}
<div id="courseSummaryContainer" class="tab">
    <table class="summaryTable courseSummary smallFontTable">
        <thead><tr>
            <th>Name</th>
            <th>Description</th>
            <th>Date of entry</th>
            <th>Rating</th>
            <th>Grade</th>
        </tr></thead>
        <tbody>
        <tr class="row-even">
            <td>Illinois</td>
            <td>Online system</td>
            <td>201602</td>
            <td>0100</td>
            <td>5</td>
        </tr>
        <tr class="row-odd">
            <td>Alabama</td>
            <td>Regional area health</td>
            <td>201606</td>
            <td>0100</td>
            <td>7</td>
        </tr>
        </tbody>
    </table>
</div>
Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • That is 100% DOM code already. (But you'd be smart to use jQuery.) Lookup "DOM". It's nothing more than a special javascript API for interfacing with a web page. In this case, `document.querySelector`. – Brock Adams Dec 24 '16 at 02:52
  • I looked up tutorials on DOM and have been reading documentation about it. I used your code and tried running it with tampermonkey with no success. Is there something wrong with my approach at this? If I want to change "Alabama" in the element to "Georgia" what commands should I use? – marshmellooooooos Dec 24 '16 at 19:52
  • That's a new question; it's different than what you asked here; and it's too broad to shoehorn here. Mark this question as answered then ask a NEW question showing exactly what your GOAL is and an MCVE of how you tried to achieve it. – Brock Adams Dec 25 '16 at 08:06