-1

Possible Duplicate:
Event handlers inside a Javascript loop - need a closure?

for i = 1 to 5
{
 var el = document.createElement('img');
 el.ondblclick = somefunction();
 mydiv.appendChild(el);
}

Seems el.ondblclick = somefunction('test'); doesn't do any insertion.

I know I can do..

el.ondblclick= function() { somefunction('test') };

.. but that doesn't work in a loop!

Community
  • 1
  • 1
Akyhne
  • 179
  • 2
  • 9

2 Answers2

3

Your syntax is wrong.

for (var i = 1; i <= 5; i++) // initialization, condition, loop step
{
    var el = document.createElement('img');
    // you do not want to call `somefunction`, but refer to it:
    el.ondblclick = somefunction;
    mydiv.appendChild(el);
}

If your problem is, that you want to use i inside the callback function:

for (var i = 1; i <= 5; i++)
{
    var el = document.createElement('img');
    el.ondblclick = (function(i) { // bind i
        return function() { // return closure
            toggleClass('svg_container_' + i);
        };
    })(i);
    mydiv.appendChild(el);
}

C.f.

Community
  • 1
  • 1
Kijewski
  • 25,517
  • 12
  • 101
  • 143
  • Problem is to do this: el.ondblclick= function() { toggleClass('svg_container_'+i) - i will be 6 and not value of i in the loop. – Akyhne Nov 09 '12 at 19:37
  • @Akyhne: Kay's second code sample should work perfectly for you; just replace `somefunction(i)` with `toggleClass('svg_container_'+i)`. – apsillers Nov 09 '12 at 19:44
  • Updated to your example (but @apsillers was faster than me :-) ) – Kijewski Nov 09 '12 at 19:45
  • It does actually, after some fiddling with the code! Isn't there a more easy way though? Seems like a lot of code for so little. – Akyhne Nov 09 '12 at 19:57
  • jQuery: `$('img').on('click', function() { $(this).toggleClass('someclass'); }).appendTo(mydiv)`. And you'd have even less to write using RightJS. – Kijewski Nov 09 '12 at 19:58
  • Well, it shouldn't be more complicated than this: el.ondblclick = somefunction(); – Akyhne Nov 09 '12 at 20:04
  • 1
    Sorry, I didn't know I had to. I thought clicking the "This was usefull" would do it. – Akyhne Nov 09 '12 at 23:00
0

Here's the final code. I personally hate when people don't post a working example

        var sCurMonthId = sMonthId.substr(4, 2);
        var cellRight = oCurRow.insertCell(-1);
        cellRight.colSpan = iNumCells;
        for (svg = 1; svg < iNumCells; svg++)
        {
            var div = document.createElement('div');
            div.className = 'svg_container';
            div.id = 'svg_container_'+svg+'_'+sMonthId;
            cellRight.appendChild(div);
            var el = document.createElement('img');
            el.style.width = svg == 5 ? '99.5%' : '49%';
            el.className = 'svg_chart';
            el.style.cssFloat = svg%2 == 0 ? 'right' : 'left';
            el.ondblclick = (function(svg) {
                return function() {
                    toggleClass('svg_container_'+svg+'_'+sMonthId, 'svg_container', 'svg_expanded');
            };
    })(svg);
            el.src = 'mySite.tld';
            div.appendChild(el);
        }
Akyhne
  • 179
  • 2
  • 9