Me and my loops again...
I'm trying to run a for loop across several divs, each being in the class "tooltipBox" but with different ids. In each of these divs is an input text field with class "tttFalloutOrder". What I want to do in the for loop is to attach a click-event-listener on each .tttFalloutOrder input field.
This is my code so far:
function installListener(elementId) {
$( "div#" + elementId + " > .tttFalloutOrder" ).on("click", function() {
alert("clicked on " + elementId);
});
}
function runSimulation() {
alert("running simulation...");
$( "#lContent h2" ).html("Simulation <b>in progress...</b>");
var agents = $( "div.tooltipBox" );
var rFOs = $( ".rFO" );
var i, j = 0;
for(i = 0, j = 0; i < agents.length, j < rFOs.length; i++, j++) {
var ttl = rFOs[j].value;
if((ttl == "undefined") || (ttl == "n")) {
continue;
} else {
// function-factory als gscheite closure,
// siehe http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example
// und http://stackoverflow.com/questions/3572480/please-explain-the-use-of-javascript-closures-in-loops?answertab=votes#tab-top
(function(i, ttl) {
var agentId = agents[i].id;
installListener(agentId);
/*
$( "div#" + agentId + " > .tttFalloutOrder" ).on("change keypress paste focus textInput input", function() {
alert(agentId + "just got changed!");
});
*/
setTimeout(function() {
$("div#" + agentId + " > div.offlineCover").fadeIn(500);
}, ttl*1000);
})(i, ttl);
}
}
$( "#lContent h2" ).html("Simulation <b>complete</b>");
}
As you can see, I am using a closure and even delegated the actual task of attaching the listener to another function, after reading in several SO-answers related to event-listeners in loops that this would help...though I honestly don't quite see how that would make any difference. Anyway, the click listeners still won't fire and frankly I don't understand what is - or rather what is not - happening here.
Thanks in advance - you people at SO have always found a way to point unknowing souls like me into the right direction, and I really appreciate that.
Update
Case closed due to my own stupidity...
First off, yes, I had an undefined member sitting in my installListener() function.
Second, the jQuery selector $( "div#" + elementId + " > .tttFalloutOrder" ) returned undefined, since the > operator selects the second element, which has the first element as a direct parent. However, since .tttFalloutOrder is an input field sitting inside a <form> tag, that is not the case...
I now scrapped the function installListener() and solved the issue with the following code:
function runSimulation() {
alert("running simulation...");
$( "#lContent h2" ).html("Simulation <b>in progress...</b>");
var agents = $( "div.tooltipBox" );
var rFOs = $( ".rFO" );
var waitUntilEvaluate = 0;
var i, j = 0;
for(i = 0, j = 0; i < agents.length, j < rFOs.length; i++, j++) {
var ttl = rFOs[j].value;
if((ttl == "undefined") || (ttl == "n")) {
continue;
} else {
// function-factory als gscheite closure,
// siehe http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example
// und http://stackoverflow.com/questions/3572480/please-explain-the-use-of-javascript-closures-in-loops?answertab=votes#tab-top
(function(i, ttl) {
var agentId = agents[i].id;
$( "div#" + agentId + " .tttFalloutOrder" ).on("input", function() {
alert(agentId + "just got changed!");
$( "div#" + agentId + " .wasChanged" ).prop("checked", true);
});
setTimeout(function() {
$("div#" + agentId + " > div.offlineCover").fadeIn(500);
}, ttl*1000);
waitUntilEvaluate = waitUntilEvaluate + ttl * 1000;
})(i, ttl);
}
}
console.log(waitUntilEvaluate);
setTimeout(function() {
$( "#lContent h2" ).html("Simulation <b>complete</b>");
evaluate();
}, waitUntilEvaluate);
}
installListeneris called (there is anif...elsestatement after all) and that you are selecting the correct element.