I would like to have a button that acts as a filter on a table, when clicked it executes a function that does the filtering, but when clicked again it undoes the filter. if it was clicked a third time it filters the table again, and so on..
the HTML is just a simple button selector:
<button id="filterButton">Click here to filter</button>
and I have this Javascript function:
window.onload = function() {
document.getElementById('filterButton').onclick = function() {
FilterTable(); //this function is the one that actually does the filtering, basically setting the tr[indexOfRow].style.display="none" on some specific rows.
};
I would like to make it so that if the button was clicked again, it would call a different function that will undo the filtering.
I tried adding a line at the very end of this function change the ID of the button to filterButtonClicked using document.getElementById("filterButton").setAttribute("id", "filterButtonClicked"); and then have another JS function that will handle the document.getElementById('filterButtonClicked').onclick event, which will undo the filtering, and then at the end of this function, change back the ID of the button to fiterButton. In theory, this will make it so that if the button was clicked for the third time, it would trigger the first JS function, which will do the filtering again and change the ID to filterButtonClicked. but in practice, this unfortunately didn't work, it would only change the ID to filterButtonClicked once, and subsequent clicks don't change it back to filterButton.
Maybe this could be implemented in a way so that if the number of clicks on the button was odd it would execute the first function, and if it was even it would execute the second one. but I don't know how to implement this.
Is there a better way to do this?