Leo
Leo

Reputation: 47

How do i get a cell from a HTML table with jquery or DOM?

I have a unchangeable HTML table with CSS. I need to get the value from the first column to filter it. I can't find the jquery or DOM function to do that,

  1. Because i couldn't figure out how to get the value of an cell, i tried to get the value of the first column.
  2. The first column is bold. I tried to get it via the bold tag, but it didn't worked.
<html>
<head>
<title>title</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="expires" content="0">
</head>

<body>
<table class="mon_head">
    <tr>

        <td valign="bottom"></td>
        <td align="right" valign="bottom">
            <p>School <span style="width:10px">&nbsp;</span> Adresse<br />
            Stundenplan 2016/2017<span style="width:10px">&nbsp;</span> <span style="width:10px">&nbsp;</span> Stand: 01.12.2017 10:12</p>
        </td>
    </tr>
</table>

<center>
<div class="mon_title">9.12.2016 Freitag</div>
<table class="info" >
<tr class="info"><th class="info" align="center" colspan="2">Nachrichten zum Tag</th></tr>
<tr class='info'><td class='info' colspan="2">Indubio Müsli</td></tr>
</table>
<p>
<table class="mon_list" >
<tr class='list'>
<th class="list" align="center"><b>Klasse(n)</b></th>
<th class="list" align="center">Stunde</th>
<th class="list" align="center">(Lehrer)</th>
<th class="list" align="center"><b>Vertreter</b></th>
<th class="list" align="center">Fach</th><th class="list" align="center">Raum</th>
<th class="list" align="center">Vertretungs-Text</th>
</tr>
<tr class='list odd'>
<td class="list" align="center"><b>5a</b></td>
<td class="list" align="center">5</td>
<td class="list" align="center">Se</td>
<td class="list" align="center"><b>Ma</b></td>
<td class="list" align="center">BNT-b</td>
<td class="list" align="center">2.25</td>
<td class="list" align="center">Vertretung</td></tr>
<tr class='list even'>
<td class="list" align="center"><b>5a</b></td>
<td class="list" align="center">6</td>
<td class="list" align="center">Se</td>
<td class="list" align="center"><b>---</b></td>
<td class="list" align="center">---</td>
<td class="list" align="center">---</td>
<td class="list" align="center">frei</td></tr>
<tr class='list odd'>
<td class="list" align="center"><b>5c</b></td>
<td class="list" align="center">1</td>
<td class="list" align="center">Mü</td>
<td class="list" align="center"><b>Au</b></td>
<td class="list" align="center">M</td>
<td class="list" align="center">1.23</td>
<td class="list" align="center">Aufgaben</td></tr>
<tr class='list even'>
<td class="list" align="center"><b>5c</b></td>
<td class="list" align="center">2</td>
<td class="list" align="center">Mü</td>
<td class="list" align="center"><b>Gi</b></td>
<td class="list" align="center">M</td>
<td class="list" align="center">1.23</td>
<td class="list" align="center">Aufgaben</td></tr>
</table>
</p>
<p id="ausgabe"></p>
</center>

<script>


function filtern(){
//Doesn't work
var klasse = "5a " + document.querySelectorAll('<b>').innerHTML; 
document.getElementById("ausgabe").innerHTML = klasse

}
</script>


</body>
</html>


The output is: 5a undefined

Upvotes: 1

Views: 80

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337637

Firstly the selector for the <b> element is just 'b', not '<b>'.

Secondly, querySelectorAll() returns a nodeList, so there's no innerHTML property on that object. As there are multiple b elements in the table you need to use a loop to build a string from them all. You can do this implicitly by using map().

Finally, in the example below, note the use of .mon_list in the selector to restrict the retrieved elements to only those within the target table and also :nth-child(1) to retrieve the first column cells.

Also, as noted by T.J. Crowder in the comments, you would be better off not relying on the existence of the b element in the cell and just reading the textContent of the td directly.

With all that said, try this:

function filtern() {
  var klasse = Array.from(document.querySelectorAll('.mon_list td:nth-child(1)')).map(function(el) {
    return el.textContent;
  }).join(', ');
  document.getElementById("ausgabe").innerHTML = klasse
}

filtern();
<table class="mon_list">
  <tr class='list'>
    <th class="list" align="center"><b>Klasse(n)</b></th>
    <th class="list" align="center">Stunde</th>
    <th class="list" align="center">(Lehrer)</th>
    <th class="list" align="center"><b>Vertreter</b></th>
    <th class="list" align="center">Fach</th>
    <th class="list" align="center">Raum</th>
    <th class="list" align="center">Vertretungs-Text</th>
  </tr>
  <tr class='list odd'>
    <td class="list" align="center"><b>5a</b></td>
    <td class="list" align="center">5</td>
    <td class="list" align="center">Se</td>
    <td class="list" align="center"><b>Ma</b></td>
    <td class="list" align="center">BNT-b</td>
    <td class="list" align="center">2.25</td>
    <td class="list" align="center">Vertretung</td>
  </tr>
  <tr class='list even'>
    <td class="list" align="center"><b>5a</b></td>
    <td class="list" align="center">6</td>
    <td class="list" align="center">Se</td>
    <td class="list" align="center"><b>---</b></td>
    <td class="list" align="center">---</td>
    <td class="list" align="center">---</td>
    <td class="list" align="center">frei</td>
  </tr>
  <tr class='list odd'>
    <td class="list" align="center"><b>5c</b></td>
    <td class="list" align="center">1</td>
    <td class="list" align="center">Mü</td>
    <td class="list" align="center"><b>Au</b></td>
    <td class="list" align="center">M</td>
    <td class="list" align="center">1.23</td>
    <td class="list" align="center">Aufgaben</td>
  </tr>
  <tr class='list even'>
    <td class="list" align="center"><b>5c</b>
    </td>
    <td class="list" align="center">2</td>
    <td class="list" align="center">Mü</td>
    <td class="list" align="center"><b>Gi</b></td>
    <td class="list" align="center">M</td>
    <td class="list" align="center">1.23</td>
    <td class="list" align="center">Aufgaben</td>
  </tr>
</table>
<p id="ausgabe"></p>

Upvotes: 3

Related Questions