- Joined
- Sep 20, 2018
- Messages
- 23,905
- SLU Posts
- 18459
What's wrong with this, please? As far as I can tell from the Firefox console debugger, it's the line
that's the problem, in that the word "cell" in that line that has a squiggly underline.
The error message is "Uncaught TypeError: cell is undefined."
I wrote this with a great deal of help from ChatGPT, so I'm not really sure what I'm doing -- the logic makes sense to me, but I don't understand the syntax too well.
ETA: I've tried adding this
and the pop-up says "0 undefined", which presumably means it's not seeing any cells, which puzzles me, because there are certainly some there. It's reading rows.length correctly.
There's a button in the first cell in each row other than the table header.
JavaScript:
// Get the button element in the cell
const button = cell.getElementsByTagName("button")[0];
The error message is "Uncaught TypeError: cell is undefined."
I wrote this with a great deal of help from ChatGPT, so I'm not really sure what I'm doing -- the logic makes sense to me, but I don't understand the syntax too well.
JavaScript:
<script>
//<![CDATA[
window.onload = function () {
// Get the table element
const table = document.getElementById(\"MyTable\");
// Get the rows of the table
const rows = table.getElementsByTagName(\"tr\");
// Loop through the rows
for (const i = 0; i < rows.length; i++) {
// Get the first cell in the row (this seems to be the problem)
const cell = rows[i].getElementsByTagName(\"td\")[0];
// Get the button element in the cell
const button = cell.getElementsByTagName(\"button\")[0];
// Set the button's ID to a unique value
button.id = \"button\" + i;
}
// Loop through the rows
for (const i = 0; i < rows.length; i++) {
// Get the first cell in the row
const cell = rows[i].getElementsByTagName(\"td\")[0];
// Get the button element in the cell
const button = cell.getElementsByTagName(\"button\")[0];
// Add an event listener to the button
document.getElementById(button.id).addEventListener(\"click\", function() {
const currentCell = this.parentNode; // this refers to the button element
const nextCell = currentCell.nextElementSibling; // get the next cell on the right
let text1 = \"PlaySample~\";
let text2 = nextCell.textContent;
let text = text1+text2;
console.log(text); // read the contents of the next cell
const xhttp = new XMLHttpRequest();
xhttp.open(\"POST\",\""+strURL+"\",true);
xhttp.send(text);
});
}
};
//]]>
</script>
JavaScript:
// Loop through the rows
for (const i = 0; i < rows.length; i++) {
// Get the first cell in the row
const cell = rows[i].getElementsByTagName("td")[0];
alert(i+" "+cell);
// Get the button element in the cell
const button = cell.getElementsByTagName("button")[0];
There's a button in the first cell in each row other than the table header.
Last edited:

