/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] funny for loop result

i have this function:

[CODE]function getT(e)
{

var totals=0
for(var k in e)
{

var count=e[k];
var p=document.createElement(“p”);
p.id=”tag_”+totals;
var dv=document.createElement(“div”)
dv.id=”iDs_”+totals;
var a=document.createElement(“a”);
a.className=”norm”;
a.href=”#”
a.onclick=function (){new GROUP(k,dv);return false;}
var placeName=document.createTextNode(k+”(“+count+”)”);
a.appendChild(placeName);
p.appendChild(a)
p.appendChild(dv)
resultTag.appendChild(p)
totals++;
}
}
[/CODE]

it works fine, except one thing…in the new GROUP method, “k” always represents the last element in “e” and dv always represents the last dv created end of the loop. However, in the placeName var, it represents the unique elements in e, and all the dv tags are numeric/proper. For example, if e where [‘mom’,’dad’,’me’], placeName would be each name respectively…but in the new Group(k,dv) call, k is always “me” and dv is always 2….ideas?

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERSep 23.2007 — I got a lot of interesting comments about this in a related thread.

See: http://www.webdeveloper.com/forum/showthread.php?t=158781
Copy linkTweet thisAlerts:
@mlechoauthorJan 23.2008 — hi all...i am back to this question. i have a similar function today, and js insists on passing the last of the loop to the onclick function. I am trying to pass the unique fields (ips[i]) to each div's onclick event handler....why am i always getting the last in the loop as the parameter being passed?



[CODE]

for(var i=0;i<ips.length;i++)
{
var thisInputEl=ips[i];
if(thisInputEl.type=="radio" )
{
var thisIp=ips[i];
//hide away radios with css
thisInputEl.className="radio_alt"
//create shells
var div=document.createElement("div")
div.className="radio_custom";
div.onclick=function (){checkUnCheck(thisInputEl);return false;}
var pt=document.getElementById(thisInputEl.id).parentNode;
pt.insertBefore(div,thisInputEl)
}
if(thisInputEl.type=="checkbox")
{
thisInputEl.className="check_alt"
}
}
[/CODE]
Copy linkTweet thisAlerts:
@Arty_EffemJan 23.2008 — hi all...i am back to this question. i have a similar function today, and js insists on passing the last of the loop to the onclick function. I am trying to pass the unique fields (ips[i]) to each div's onclick event handler....why am i always getting the last in the loop as the parameter being passed?

[/QUOTE]




Add this function to your code:

[CODE]function getFuncRef( p )
{
return function(){checkUnCheck( p );return false;}

}
[/CODE]


Then replace[CODE]
div.onclick=function (){checkUnCheck(thisInputEl);return false;}[/CODE]

with[CODE]div.onclick = getFuncRef(thisInputEl);[/CODE]

Another way to achieve the same thing is just to replace
[CODE]
div.onclick=function (){checkUnCheck(thisInputEl);return false;}[/CODE]
With[CODE]div.onclick=new Function("checkUnCheck("+thisInputEl+");return false;");[/CODE]
Copy linkTweet thisAlerts:
@KorJan 23.2008 — the explanation is simple. The moment you do onclick and call the function, the loop is already ended, and its incremented variable k has it's last, maximum, value.

To "catch" the increment values on each step of the loop you may create a [COLOR="Blue"]custom property[/COLOR] for each element and use a [COLOR="DarkGreen"]closure[/COLOR]

for(var k=0;k<elements.length;k++){

elements[k].[COLOR="Blue"]ind[/COLOR]=k;

elements[k].onclick=function(){alert([COLOR="DarkGreen"]this[/COLOR].[COLOR="Blue"]ind[/COLOR])}

}
Copy linkTweet thisAlerts:
@mlechoauthorJan 24.2008 — OH MY GOSH!!! I finally understand....thanks guys
×

Success!

Help @mlecho spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...