/    Sign up×
Community /Pin to ProfileBookmark

problem feeding variable in event handler

hello

I’m new to Javascript and I have just got myself into a dead end. My problem is that when I attach a function to an onclick handler of an element I cannot seem to be able to feed it with a variable. It will only get the last value, when the loop has ended. Here is the code. The first lines are initialization and HTML element generation, my problem is very simple to understand and lies in the last lines of this code. All the ’tile’ elements will have a onclick=printMe(9) event, but i want it to be printMe(1), printMe(2)…printMe(9) for my 9 tiles.

window.onload=init;

function init() {
new Board();
}

function Board() {
DOMboard=document.getElementById(‘board’);
for(i=0; i<9; i++) {
tile = document.createElement(‘a’);
tile.setAttribute(‘href’, ‘#’);
tile.appendChild(document.createTextNode(i));
DOMboard.appendChild(tile);

//problem here, no matter which ’tile’ i click
//it will always print 9 which is the last
//value of i after the loop has ended.
tile.onclick=function(){printMe(i)};
}

function printMe(z) {
alert(z);
}

}

Regards

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@HaganeNoKokoroJun 11.2005 — One way to accomplish the effect is to put the data in the element's id like sofunction Board() {
DOMboard=document.getElementById('board');
for(i=0; i&lt;9; i++) {
tile = document.createElement('a');
tile.setAttribute('href', '#');
tile.appendChild(document.createTextNode(i));
[color=red]tile.id="tl"+i;[/color]
DOMboard.appendChild(tile);

<i> </i>[color=red]tile.onclick=printMe;[/color]
}
}
function printMe() {
[color=red]alert(this.id.substring(2));[/color]
}
Copy linkTweet thisAlerts:
@acorbelliJun 11.2005 — [CODE]
window.onload=init;

function init()
{
new Board();
}

function Board()
{
DOMboard=document.getElementById('board');

for(i=0; i<9; i++)
{
tile = document.createElement('a');
tile.setAttribute('href', '#');
tile.appendChild(document.createTextNode(i));
DOMboard.appendChild(tile);

//problem here, no matter which 'tile' i click
//it will always print 9 which is the last
//value of i after the loop has ended.
tile.onclick=function(){printMe(i)};
}
}

function printMe(z) {
alert(z);
}

[/CODE]


Try that...you had it nested strangely.
×

Success!

Help @stgma 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 4.30,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...