forked from alex/nyt-2020-election-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
62 lines (52 loc) · 1.83 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'use strict';
// Add event handler
const searchField = document.getElementById('myInput');
searchField.addEventListener('keyup', search);
const reset = document.getElementById('reset');
reset.addEventListener('click', showAll);
const hide = document.getElementById('hide');
hide.addEventListener('change', hideRows);
function search() {
// get variables
let input = document.getElementById('myInput');
let filter = input.value.toLowerCase();
let tables = document.getElementsByTagName('table');
console.log('filter', filter)
for(let i = 0; i < tables.length; i++) {
let textValue = tables[i].id;
if (textValue.toLowerCase().indexOf(filter) > -1) {
tables[i].style.display = '';
} else {
tables[i].style.display = 'none';
}
}
};
function hideRows(e) {
// hides all but three rows in each table for readability
let number = parseInt(e.target.value);
let tables = document.getElementsByTagName('table');
showAll();
for (let i = 0; i < tables.length; i++) {
// get a count of the rows in each table
let rows = tables[i].getElementsByTagName('tbody')[0].getElementsByTagName('tr');
console.log(rows);
if(rows.length > number) {
for(let j = number; j < rows.length; j++) {
rows[j].style.display = 'none';
}
}
}
}
function showAll() {
console.log('hi')
let tables = document.getElementsByTagName('table');
for (let i = 0; i < tables.length; i++) {
// get a count of the rows in each table
let rows = tables[i].getElementsByTagName('tbody')[0].getElementsByTagName('tr');
console.log(rows);
for(let j = 0; j < rows.length; j++) {
rows[j].style.display = '';
console.log(rows[j]);
}
}
}