Addy2311 Addy2311 - 1 year ago 44
CSS Question

Filter/search table doesn't work

I've created a page where I can save actors. They are stored in an array.
And after saving these data, I'm creating a table where I'm inserting the data.

I wanted to create a search element where I can filter my data by name. But it didn't work.

Maybe someone of you knows why.

window.onload = function() {
var allArtists = [];

$('#submit').click(function() {
var $rowTemplate = $('<tr><td data-id="id"></td><td data-id="name"></td><td data-id="geburtsort"></td><td data-id="geburtsdatum"></td><td data-id="favorite"></td></tr>');

var artistName = $("#name").val();
var ort = $("#ort").val();
var datum = $("#datum").val();
var favourite = $("[name=Favorit]").is(':checked');

allArtists.push([artistName, ort, datum]);

var rowId = allArtists.length;


var checked = favourite ? "checked" : "";

$rowTemplate.find('[data-id=favorite]').text(favourite).append($('<input type="checkbox" id="fave" ' + checked + '>'));
$("#table tbody").append($rowTemplate);

function myFunction() {
var input, filter, table, tr, td, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
table = document.getElementById("table");
tr = table.getElementsByTagName("tr");

// Loop through all table rows, and hide those who don't match the search query
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[0];
if (td) {
if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";

body {
font-family: Arial, Helvetica;
font-size: 13px;

#myInput {
background-position: 10px 12px;
background-repeat: no-repeat;
width: 20%;
font-size: 16px;
padding: 5px;
border: 1px solid #ddd;
margin-bottom: 12px;

#table {
border-collapse: collapse;
width: 60%;
border: 1px solid #ddd;
font-size: 14px;

#table th, #myTable td {
text-align: left;
padding: 12px;

#table tr {

border-bottom: 1px solid #ddd;

#table tr.header, #table tr:hover {

background-color: #f1f1f1;

<script src=""></script>
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<script src="js/jquery-3.1.1.min.js"></script>
<script src="js/main.js"></script>
<link rel="stylesheet" href="css/style.css">
<h1>Künstler hinzufügen</h1>
<form id="send">
<label>Name des Künstlers</label>
<input id="name" type="text" placeholder="Name des Künstlers" />

<input id="ort" type="text" placeholder="Woher stammt der Künstler" />

<input id="datum" type="text" placeholder="Wann ist der Künstler geboren?" />

<input type="checkbox" name="Favorit" value="Favorit">Favorit

<input type="button" id="submit" name="senden" value="Senden">

<input type="text" id="myInput" onkeyup="myFunction()" placeholder=" Search for names..">

<table id="table">


Answer Source

In function myFunction


td = tr[i].getElementsByTagName("td")[0];


td = tr[i].getElementsByTagName("td")[1];

You wanna check the name, use [0] check the id