The idea is simple: don't prevent events. Don't make your code overly complex and error-prone by registering and unregistering any events. Add all event handlers only once and never change them.
Instead, do much simpler thing: write the event handlers the way they do different things depending on some shared object used to control it. Simply put, have a shared flag object in the outer scope in relation to the handler functions. Then one handler, such as the click on your button, can modify this flag object to "don't handle onchange". The
onchange
handle will read this state and them simply return. This way, the
onchange
event will be invoked in all cases, but it will do nothing if the flag tells it so.
In simplest form, it could look like
var enableButton =
var disableButton =
var input =
var handleOnChange = true;
disableButton.onclick = function() { handleOnChange = false; }
enableButton.onclick = function() { handleOnChange = true; }
input.onchange = function() {
if (!handleOnChange) return;
doSomethingOnChange();
}
Isn't that simple?
—SA