tags:

views:

105

answers:

1

Hello, Here is my code snippt. But the code is breaking after inner for loop. But getting no error message. Any idea?

Thanks.

    var lastnames   = document.getElementsByClassName('box_nachname');
    var firstnames      = document.getElementsByClassName('box_vorname');
    var teilnehmer  = document.getElementsByClassName('select');
    observers = [];

    // iterate over nachname array.
    for (var i = 0; i < lastnames.length; i++) {

        // Create an observer instance.
        observers[i] = new Observer();


        // Subscribe oberser object.
        for(idx in teilnehmer) {
            if(teilnehmer[idx].id.split("_")[0].toLowerCase() !== "zl") {
                var anynum = function(element) {
                                             observers[i].subscribe(element, updateTeilnehmerSelectbox);
                                         }(teilnehmer[idx]);
            }
        }


        //on blur the Observer fire the updated info to all the subscribers.
        var anynumNachname = function(j, element, value, observer) {
                                            cic.addEvent(lastnames[j], 'blur', observer.fire(element, value));
                                            } (i, lastnames[i], lastnames[i].value, observers[i]);
        cic.addEvent(firstnames[i], 'blur', function(element, value, observer) {observer.fire(element, value)}(lastnames[i], lastnames[i].value, observers[i]));

    }
+2  A: 

You're using the loop variable "i" in the "addEvent" call. That won't work properly because every one of the event handlers will share the same "i" and so each will only see the last value that "i" was set to.

cic.addEvent(firstnames[i], 'blur', (function(index) {
  return function(element, value, observer) {
    observer.fire(element, value)}(lastnames[index], lastnames[index].value, observers[index]);
  };
})(i));

Also, though I'm not sure this is necessary, I'd put the function you're calling for "anynumNachname" in parenthesis:

var anynumNachname = (function(j, element, value, observer) {
  cic.addEvent(lastnames[j], 'blur', observer.fire(element, value));
})(i, lastnames[i], lastnames[i].value, observers[i]);
Pointy
Regarding wrapping `anyNachname` in parens: there's a JSLint option to "Require parens around immediate invocations" which requires parens around the entire right-hand side of the assignment, not just the function. So the parens around the function itself are superfluous; I guess you were thinking of wrapping the entire invocation?
Matt Ball
No, I'm in the habit of wrapping the function definition itself because it's required when your expression starts with a function definition. I suppose when it's unambiguous that the function definition is just an r-value, the parens aren't needed.
Pointy
To clarify that comment: you need to explicitly make the Javascript parser understand that you're not declaring a function in the case that you want an anonymous function for the purpose of an immediate call. (I know you know that, @Bears, but the clarification is for posterity :-)
Pointy