Click here to Skip to main content
15,901,870 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all I have this code in javascript

for (var i = 0, link; i < 5; i++) {
var div = document.createElement("div");
var link = document.createElement("a");
link.innerHTML = "Link " + i;
link.onclick = function () {
alert(i);
};
div.appendChild(link);
document.body.appendChild(div);
}

Problem



When you click on the links, the alert box always says 5. What would you change in the javascript to fix this?





when I click the link it gives me 5 I need when I click the link give me the number of the link any body tell me the error ??


I don't want to use link.onclick = function (event) { alert(event.target.innerHTML); };

What I have tried:

I tried LOT OF THINGS BUT with no true result
Posted
Updated 4-Oct-16 4:35am

JavaScript closure inside loops – simple practical example - Stack Overflow[^]

JavaScript
function createlink(i) {
    var link = document.createElement("a");
    link.innerHTML = "Link " + i;
    link.onclick = function () {
        alert(i);
    };

    return link;
}

for (var i = 0, link; i < 5; i++) {
    var div = document.createElement("div");
    var link = createlink(i);
    div.appendChild(link);
    document.body.appendChild(div);
}
 
Share this answer
 
v2
Comments
Sarah Mohammed77 4-Oct-16 9:21am    
I do it like that but it still gives me 5

var funcs = [];
function createfunc(i) {
return function() { console.log("My value: " + i); };
}
for (var i = 0, link; i < 5; i++)
{
funcs[i] = createfunc(i);
var div = document.createElement("div");
var link = document.createElement("a");
link.innerHTML = "Link " + i;
link.onclick = function () {
alert(i);
};
div.appendChild(link);
document.body.appendChild(div);
}
F-ES Sitecore 4-Oct-16 9:57am    
You didn't do anything that changes your code, all you did was add a call to a function, your problem code remains.

The intention was to read the article and understand what it is teaching you and then applying that understanding to your code. You'll never be a good developer just hoping that someone random site will have random code you can paste that magically fixes your code.
The reason it is always 5 is because your loop increments i to 5 and you alert that same variable.

You said,
Quote:
I don't want to use link.onclick = function (event) { alert(event.target.innerHTML); };
Why not? Something along those lines is the correct way to do it.
 
Share this answer
 
Comments
Sarah Mohammed77 4-Oct-16 9:56am    
because I don't need this solution
ZurdoDev 4-Oct-16 9:57am    
That IS the solution. Why don't you need it?
try this

JavaScript
for (var i = 0, link; i < 5; i++) {
                
                 var div = document.createElement("div");
                 var link = document.createElement("a");
                 link.innerHTML = "Link " + i;
                 link.onclick = function () {                     
                     alert(this.innerText.split(' ')[1]);
                 };
                 div.appendChild(link);
                 document.body.appendChild(div);
             }
 
Share this answer
 
Comments
Sarah Mohammed77 4-Oct-16 10:52am    
not give result

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900