I try to find small real life problems and attempt to write code to make my life easier, one of them being a search-table simple program that sorts data based on user input.
JavaScript
x
102
102
1
<!DOCTYPE html>
2
<html>
3
<head>
4
<meta name="viewport" content="width=device-width, initial-scale=1">
5
<style>
6
* {
7
box-sizing: border-box;
8
}
9
10
#myInput {
11
background-image: url('/css/searchicon.png');
12
background-position: 10px 10px;
13
background-repeat: no-repeat;
14
width: 100%;
15
font-size: 16px;
16
padding: 12px 20px 12px 40px;
17
border: 1px solid #ddd;
18
margin-bottom: 12px;
19
}
20
21
#myTable {
22
border-collapse: collapse;
23
width: 100%;
24
border: 1px solid #ddd;
25
font-size: 18px;
26
}
27
28
#myTable th, #myTable td {
29
text-align: left;
30
padding: 12px;
31
}
32
33
#myTable tr {
34
border-bottom: 1px solid #ddd;
35
}
36
37
#myTable tr.header, #myTable tr:hover {
38
background-color: #f1f1f1;
39
}
40
</style>
41
</head>
42
<body>
43
44
<h2>My Tables</h2>
45
46
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name">
47
48
<table id="myTable">
49
<tr class="header">
50
<th style="width:60%;">Code</th>
51
<th style="width:40%;">Description</th>
52
</tr>
53
<tr>
54
<td>1</td>
55
<td>Description 1</td>
56
</tr>
57
<tr>
58
<td>2</td>
59
<td>Description 2</td>
60
</tr>
61
<tr>
62
<td>3</td>
63
<td>Description 3</td>
64
</tr>
65
<tr>
66
<td>4</td>
67
<td>Description 4</td>
68
</tr>
69
<tr>
70
<td>5</td>
71
<td>Description 5</td>
72
</tr>
73
<tr>
74
<td>6</td>
75
<td>Description 6</td>
76
</tr>
77
</table>
78
79
<script>
80
function myFunction() {
81
var input, filter, table, tr, td, i, txtValue;
82
input = document.getElementById("myInput");
83
filter = input.value.toUpperCase();
84
table = document.getElementById("myTable");
85
tr = table.getElementsByTagName("tr");
86
for (i = 0; i < tr.length; i++) {
87
td = tr[i].getElementsByTagName("td")[1];
88
if (td) {
89
txtValue = td.textContent || td.innerText;
90
if (txtValue.toUpperCase().indexOf(filter) > -1) {
91
tr[i].style.display = "";
92
} else {
93
tr[i].style.display = "none";
94
}
95
}
96
}
97
}
98
</script>
99
100
</body>
101
</html>
102
And my question is, is there a more efficient way to import data rather than having to write.
JavaScript
1
5
1
<tr>
2
<td>6</td>
3
<td>Description 6</td>
4
</tr>
5
like 1000 times? I would appreciate guidance rather than someone just solving my issue. My aim is to have fun and learn at the same time. Seems though I’m asking google to wrong questions and going down rabbit holes I’ve no business being in and confusing myself even more.
Thank you for your time reading this and ty in advance on any feedback I may receive 🙂
Advertisement
Answer
js
JavaScript
1
8
1
var dotimes = 1000
2
3
for(i=0;i<dotimes;i++){
4
//time to make a list
5
const node = document.createElement("li");
6
//add it to the body
7
document.getElementById("body").appendChild(node)}
8
html
JavaScript
1
2
1
<body id="body">
2