Apply function in loop

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Apply function in loop

Post by Jonah Bron »

Hello, world!

This is an issue that first showed it's ugly head in a Javascript project, and now has returned to haunt me in ActionScript. This is the scenario:

Code: Select all

var elements = document.getElementsByTagName('a');
for (var i=0;i<elements.length;i++){
    elements[i].onclick = function (){
        alert(this.getAttribute('href'));
    }
}
What happens: Whichever link you click, it will alert the href of the last link that was looped through. Is there a way to get around this?

This is a fictional snippet of code
User avatar
slightlymore
Forum Newbie
Posts: 9
Joined: Sun May 18, 2008 5:24 pm
Location: Oxford, England

Re: Apply function in loop

Post by slightlymore »

try using ++i instead of i++
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Apply function in loop

Post by VladSun »

slightlymore wrote:try using ++i instead of i++
???
Doesn't make any sense.

@PHPyoungster - I didn't unserstand you - this is a javascript or an actionscript problem you have?
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Apply function in loop

Post by Jonah Bron »

That was just an example. This problem applies to actionscript and javascript alike. When you apply a function object to an object in a loop, they all end up with the same function as the last one looped through. Does that make sense?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Apply function in loop

Post by VladSun »

I've tried (with IE, FF) the code you posted - it works correctly. I can't reproduce the problem you describe.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
HCBen
Forum Commoner
Posts: 33
Joined: Thu Jun 22, 2006 3:15 pm
Location: Indiana

Re: Apply function in loop

Post by HCBen »

Try adding a break after the alert. You have to stop the loop when the element is triggered.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Apply function in loop

Post by VladSun »

HCBen wrote:Try adding a break after the alert. You have to stop the loop when the element is triggered.

???
It's a function definition you are suggesting to place the break in...
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Apply function in loop

Post by pickle »

slightlymore wrote:try using ++i instead of i++
Makes perfect sense. http://ca3.php.net/manual/en/language.o ... rement.php
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Apply function in loop

Post by VladSun »

pickle wrote:
slightlymore wrote:try using ++i instead of i++
Makes perfect sense. http://ca3.php.net/manual/en/language.o ... rement.php
:)
Yes, it would decrease the time for 1 000 000 loops with 0.002sec :)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Apply function in loop

Post by pickle »

slightlymore wrote:try using ++i instead of i++
I'm guessing he thought switching the incrementer would change the value of i in the loop. It does not. However, you can see a concrete example of how changing the order of the incrementer & the variable does matter:

Code: Select all

var i = 1;
var j = 1;
alert(i++);
alert(++j);
That'll alert '1', then '2'
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Apply function in loop

Post by VladSun »

pickle wrote:I'm guessing he thought switching the incrementer would change the value of i in the loop. It does not.
My guess also :)
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply