In this code, the #searchInpForm
search box gets cleared but the data that is called by my getAllPersonnel()
function doesn’t refresh. It refreshes if I don’t include the line about emptying the search box.
Also, the function gets called but the data just doesn’t. I have the same issue if I set the search input field to an empty string as well. What should I look at next to fix this?
$("#refreshBtn").click(function () {
if ($("#personnelBtn").hasClass("active")) {
// console.log("Button was clicked");
$("#searchInpForm")[0].reset();
getAllPersonnel();
// $("#searchInp").val("");
} else if ($("#departmentsBtn").hasClass("active")) {
// console.log("Button was clicked");
getAllDepartments();
} else if ($("#locationsBtn").hasClass("active")) {
getAllLocations();
}
});
2
Answers
I assume your
getAllPersonnel()
function is using thesearchInpForm
value. So if you clear it first, the function won’t work/refresh. Try to clear it only after function is called, like below.Or, clear it after a few milliseconds.
You are emptying the search box before you apply the search. Switch the order of the operations:
Also, it would make sense to get the value/text of your search and pass it to
getAllPersonnel
, so it will rely on the search you have given it, instead of what the UI button has. This way your UI will not mess around in your logic.