|
|
|
Register | FAQ | Members List | Calendar | Today's Posts | Search |
JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...) |
|
Thread Tools | Rate Thread | Display Modes |
#1
Yesterday, 10:57 AM
|
|||
|
|||
Please tell me why this is happening!
Please tell me why I am constantly getting "alert(10)". How do I modify this code so the alert is unique to the link. Every child should be unique with a unique onclick function. This isn't so!
<html> <head> <script> function test() { for(var a=0;a<10;a++) { var elm = document.createElement("a"); elm.innerHTML = " " + a + " "; elm.onclick = function(){alert(a)}; document.body.appendChild(elm); } } </script> </head> <body> </body> </html> |
#2
Yesterday, 11:31 AM
|
|||
|
|||
Your code won't work because this line:
Code:
elm.onclick = function(){alert(a)}; Using PHP highligher, but it's all JS: PHP Code:
|
#3
Yesterday, 11:33 AM
|
|||
|
|||
Thanks for the quick reply. I fiddled with it some more here is what I came up with also.
<html> <head> <script> function test() { for(var a=0;a<3;a++) { var elm = document.createElement("a"); elm.innerHTML = " " + a + " "; elm.name = a; elm.onclick = function(){alert(this.name)}; document.body.appendChild(elm); } } </script> </head> <body> </body> </html> |
#4
Yesterday, 12:13 PM
|
|