JavaScript and client side scripting.
Moderator: General Moderators
Jonah Bron
DevNet Master
Posts: 2764 Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California
Post
by Jonah Bron » Tue May 20, 2008 7:46 pm
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
slightlymore
Forum Newbie
Posts: 9 Joined: Sun May 18, 2008 5:24 pm
Location: Oxford, England
Post
by slightlymore » Wed May 21, 2008 7:16 am
try using ++i instead of i++
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Wed May 21, 2008 8:33 am
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
Jonah Bron
DevNet Master
Posts: 2764 Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California
Post
by Jonah Bron » Wed May 21, 2008 4:59 pm
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?
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Wed May 21, 2008 6:45 pm
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
HCBen
Forum Commoner
Posts: 33 Joined: Thu Jun 22, 2006 3:15 pm
Location: Indiana
Post
by HCBen » Wed May 21, 2008 7:17 pm
Try adding a break after the alert. You have to stop the loop when the element is triggered.
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Thu May 22, 2008 4:06 am
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
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Thu May 22, 2008 2:40 pm
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
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Thu May 22, 2008 2:51 pm
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.
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Thu May 22, 2008 2:54 pm
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