I'm listening for changes in two variables height
and weight
. When both evaluate to true I expected the return a value of false
to stop the listener. It keeps appending <script type="text/javascript" src="js/modelers/average_aboveaverage.js"></script>
when I move a range slider elsewhere in my code.
function makeModeler() {
document.addEventListener("change", function(){
var height = document.getElementById('height-class').value;
var weight = document.getElementById('weight-class').value;
shape = height + "_" + weight;
if (!(height && weight)) {
return true;
} else {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "js/modelers/"+shape+".js";
document.body.appendChild(script);
return false;
}
});
}
makeModeler();
document.addEventListener("change", makeModeler);
document.removeEventListener("change", makeModeler, false);
You are adding the event infinitely in every change since you are doing:
And the
makeModeler
function also adds at first thatonChange
event