Skip to content

Instantly share code, notes, and snippets.

@Octagon-simon
Created June 30, 2022 02:31
Show Gist options
  • Select an option

  • Save Octagon-simon/5968d1ef06381facdbb75dc46b66029c to your computer and use it in GitHub Desktop.

Select an option

Save Octagon-simon/5968d1ef06381facdbb75dc46b66029c to your computer and use it in GitHub Desktop.
//attach event listener
document.querySelector('#inp_search').addEventListener('input', function(){
//store the search query
let value = this.value.trim();
//check if value is not empty
if(value){
//store matching record IDs
let matchingIDs = [];
//loop index
let ind = 1;
//loop through the data to find matching text
while(ind < Object.keys(tableData).length){
//perform case-insensitive search on the string
if(tableData[ind][0].match(new RegExp(value, 'i')) ||
tableData[ind][1].match(new RegExp(value, 'i')) ||
tableData[ind][2].match(new RegExp(value, 'i')) ||
tableData[ind][3].match(new RegExp(value, 'i'))){
//store the id of the record
matchingIDs.push(ind);
}
//increment index
ind++;
}
//invoke the function by passing in the matching IDs
buildTable(matchingIDs);
}else{
//invoke the build table function without providing an argument
buildTable();
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment