I have a simple table filled with rows from a MySQL database, the HTML part looks like this
<tbody>
<?php while($row = mysqli_fetch_row($result)){ ?>
<tr class="rows" id="<?php echo $row[0]; ?>"> // the id is an auto incremented int
<td id="col1"><?php echo $row[1]; ?></td>
<td id="col2"><?php echo $row[2]; ?></td>
<td id="col3"><?php echo $row[3]; ?></td>
<td id="col4"><?php echo $row[4]; ?></td>
<td id="col5"><?php echo $row[5]; ?></td>
</tr>
<?php } ?>
</tbody>
It gets the job done and I have a nice table. Now I want that if I click on a row, i get five variables with the values of the cells.
The problem what stopped me was to get the <td> element. This is what I tried in the external javascript file:
var rows = document.getElementsByClassName('rows');
for (var i=0; i<rows.length; i++){
rows[i].onclick = function(){
console.log(this.getElementById('col1')); // I tried to print it first
}
}
The console responded this: Uncaught TypeError: undefined is not a function.
Then I tried console.log(rows[i].getElementById(vonalkod));, but it responded Uncaught TypeError: Cannot read property 'getElementById' of undefined
If I only write console.log(this); then it prints what I expect, the whole row in html.
So my question is that what is the syntax to get those cells by ID?