-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!

4
  • What does it do instead? Any errors? Commented Nov 9, 2012 at 19:22
  • Your loop has some strange Pascal syntax. See the JS syntax here: stackoverflow.com/questions/52080/… Commented Nov 9, 2012 at 19:22
  • 2
    Any chance you're using i inside the function? Commented Nov 9, 2012 at 19:25
  • Maybe you should look into using jQuery and similar libraries. It really makes event handling a lot easier. Commented Nov 9, 2012 at 19:56

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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: Kay's second code sample should work perfectly for you; just replace somefunction(i) with toggleClass('svg_container_'+i).
Updated to your example (but @apsillers was faster than me :-) )
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.
Sorry, I didn't know I had to. I thought clicking the "This was usefull" would do it.
|
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);
        }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.