Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #21713

Re: I don't want to have if for each index in the loop

From "Tony Johansson" <johansson.andersson@telia.com>
Newsgroups comp.lang.javascript
Subject Re: I don't want to have if for each index in the loop
Date 2013-10-24 09:16 +0200
Organization A noiseless patient Spider
Message-ID <l4ahg3$7ba$1@dont-email.me> (permalink)
References <l49n7g$u0a$1@dont-email.me> <0.2881687aedec8c956bad.20131024024418BST.87li1jbfyl.fsf@bsb.me.uk>

Show all headers | View raw


Many thanks nice solution and it was exactly this that I was looking for.

//Tony

"Ben Bacarisse" <ben.usenet@bsb.me.uk> skrev i meddelandet 
news:0.2881687aedec8c956bad.20131024024418BST.87li1jbfyl.fsf@bsb.me.uk...
> "Tony Johansson" <johansson.andersson@telia.com> writes:
>
>> I have two functions InitThumbnails and ShowBigImage shown below. I
>> want to create event handlers for each element when I loop through the
>> thumbnailsArr by using only one statement like this
>> element.addEventListener("click", function () {
>> ShowBigImage(thumbnailsArr[i]); }, false);
> <snip>
>> I think I need to use something called closure but I don't know how to
>> write ?
>>
>> function InitThumbnails(thumbnailsArr)
>> {
>>   for (var i = 0; i < thumbnailsArr.length; i++)
>>   {
>>       var element = document.getElementById("fig" + i);
>>       element.setAttribute("src", "img/" + thumbnailsArr[i]);
>>       element.setAttribute("alt", thumbnailsArr[i].split(".")[0]);
>>       document.getElementById("desc" + i).innerHTML =
>> thumbnailsArr[i].split(".")[0];
>
>    element.addEventListener("click",
>            (function (idx) {
>                return function () { ShowBigImage(thumbnailsArr[idx]); }
>                })(i),
>            false);
>
>>   }
>> }
>
> The gist of it (and I am twisting the terminology a lot here) is that in
> you previous attempt, all the functions created in the loop capture the
> same thing 'i' -- so they share it.  You need a function whose
> environment has a binding for a copy of the value of 'i'.  One way to do
> that is to call a function that returns the function you need, passing
> it the value you want to bind to a new identifier.  Each of the many
> event handlers end up with an environment with its own 'idx', each one
> with a different value.
>
> By the way, what you wrote before was also a closure -- it simply did
> not have the right bindings in its environment.
>
> <snip>
> -- 
> Ben. 

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

I don't want to have if for each index in the loop "Tony Johansson" <johansson.andersson@telia.com> - 2013-10-24 01:47 +0200
  Re: I don't want to have if for each index in the loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-10-24 02:44 +0100
    Re: I don't want to have if for each index in the loop "Tony Johansson" <johansson.andersson@telia.com> - 2013-10-24 09:16 +0200
      Re: I don't want to have if for each index in the loop Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-10-24 12:17 +0200
        Re: I don't want to have if for each index in the loop dhtml <dhtmlkitchen@gmail.com> - 2013-10-25 14:52 -0700
          Re: I don't want to have if for each index in the loop Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-10-26 00:38 +0200

csiph-web