Currently, I'm in the process of setting up a search bar within a table that has been populated by looping data retrieved from an API. My goal is to allow users to search for specific entries either by name or email. Unfortunately, I seem to be encountering an issue as the console displays an uncaught ReferenceError: sBar is not defined at window.onload. Please bear in mind that I am new to JavaScript and struggling with this problem. I apologize if this is trivial, but despite my best efforts, I can't seem to identify the mistake.
Below is my HTML:
<body>
<div>
<label for="finder">Find User:</label>
<input type="search" id="searchInput" name="sInput" placeholder="Search
user">
<button id="sButton">Search</button>
</div>
<table class="table table-responsive">
<thead class="thead-dark">
<tr>
<th scope="col">Id</th>
<th scope="col">Name</th>
<th scope="col">Username</th>
<th scope="col">Email</th>
<th scope="col">Address</th>
<th scope="col">Phone</th>
<th scope="col">Website</th>
<th scope="col">Company</th>
</tr>
</thead>
<tbody name="tTable">
</tbody>
</table>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js">
</script>
<script src="script.js">
</script>
My JavaScript code:
window.onload = function(){
let uList = document.querySelector('[name=tTable]');
fetchCall('https://jsonplaceholder.typicode.com/users', getUsers);
sButton.addEventListener('click', fetchCall('https://jsonplaceholder.typicode.com/users', sBar), false);
function sBar(getObject) {
let sUser = getObject;
let inputBar = document.getElementById("searchInput");
let text = inputBar.textContent;
let textView = text.toUpperCase();
for (let i = 0; i < getObject.length; i++) {
let uObject = sUser[i];
if (textView == uObject.name || textView == uObject.email) {
let new_tTable = document.createElement('tbody');
uList.parentNode.replaceChild(new_tTable, uList)
let row = uList.insertRow();
let idInput = document.createElement('td');
let nameInput = document.createElement('td');
let usernameInput = document.createElement('td');
let emailInput = document.createElement('td');
let cityInput = document.createElement('td');
let phoneInput = document.createElement('td');
let websiteInput = document.createElement('td');
let companyInput = document.createElement('td');
idInput.textContent = uObject.id;
nameInput.textContent = uObject.name;
usernameInput.textContent = uObject.username;
emailInput.textContent = uObject.email;
cityInput.textContent = uObject.address.city;
phoneInput.textContent = uObject.phone;
websiteInput.textContent = uObject.website;
companyInput.textContent = uObject.company.name;
row.appendChild(idInput);
row.appendChild(nameInput);
row.appendChild(usernameInput);
row.appendChild(emailInput);
row.appendChild(cityInput);
row.appendChild(phoneInput);
row.appendChild(websiteInput);
row.appendChild(companyInput);
} else {
alert("User not found");
}
}
}
function fetchCall(url, fn){
fetch(url)
.then(function(response){
return response.json();
})
.then(function(endPoint){
fn(endPoint);
})
.catch(function(error){
console.error(error);
})
}
function getUsers(getObject) {
let user = getObject;
for (let i = 0; i < getObject.length; i++) {
let userObject = user[i];
let row = uList.insertRow();
let idInput = document.createElement('td');
let nameInput = document.createElement('td');
let usernameInput = document.createElement('td');
let emailInput = document.createElement('td');
let cityInput = document.createElement('td');
let phoneInput = document.createElement('td');
let websiteInput = document.createElement('td');
let companyInput = document.createElement('td');
idInput.textContent = userObject.id;
nameInput.textContent = userObject.name;
usernameInput.textContent = userObject.username;
emailInput.textContent = userObject.email;
cityInput.textContent = userObject.address.city;
phoneInput.textContent = userObject.phone;
websiteInput.textContent = userObject.website;
companyInput.textContent = userObject.company.name;
row.appendChild(idInput);
row.appendChild(nameInput);
row.appendChild(usernameInput);
row.appendChild(emailInput);
row.appendChild(cityInput);
row.appendChild(phoneInput);
row.appendChild(websiteInput);
row.appendChild(companyInput);
}
}
}