I need to come up with a solution for reusing a function triggered by an event binding.
This problem stems from browsers remembering checkbox states, which is why I have to call the function on document load.
One approach could involve wrapping setGrid()
in an anonymous function and passing the element as a parameter, but that might complicate the process of unbinding the specific event.
Here's the HTML code:
<input type="checkbox" value="grid" id="checkbox-grid">
And the corresponding Javascript code:
function setGrid(e) {
var $this = $(e.target); // "error: e is undefined" in situation 2
if( $this.is(':checked') ){
// do something when checkbox is checked
}
else{
// do something when checkbox is not checked
}
}
// 1. event binding
$('#checkbox-grid').on('change', setGrid);
// 2. function triggered on page load
setGrid();