The currently accepted answer doesn't always work.
(To read about the problem and circumstances, read this: Defined function is "Not defined".)
So, you have 3 options:
1 (it has above-mentioned drawback)
<input type="checkbox" onchange="doAlert(this)">
<script>
function doAlert(checkboxElem) {
if (checkboxElem.checked) {
alert ('hi');
} else {
alert ('bye');
}
}
</script>
2 and 3
<input type="checkbox" id="foo">
<script>
function doAlert() {
var input = document.querySelector('#foo');
// input.addEventListener('change', function() { ... });
// or
// input.onchange = function() { ... };
input.addEventListener('change', function() {
if (input.checked) {
alert ('hi');
} else {
alert ('bye');
}
});
}
doAlert();
</script>