spacer
spacer
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    spacer   WebDeveloper.com > Client-Side Development > JavaScript
    spacer Please tell me why this is happening!
    Register FAQ Members List Calendar Today's Posts

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    spacer
     
    Thread Tools Rate Thread Display Modes
      #1  
    spacer Yesterday, 10:57 AM
    faithless spacer
    Registered User
     
    Join Date: Nov 2011
    Posts: 19
    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>
    spacer
      #2  
    spacer Yesterday, 11:31 AM
    George88 spacer
    Registered User
     
    Join Date: Sep 2011
    Posts: 75
    Your code won't work because this line:

    Code:
    elm.onclick = function(){alert(a)};
    Is telling JavaScript to alert the current value of 'a', which ends at 10. I've written some code to achieve what I think you would like. I've made it a little more obvious that the numbers are indeed wrapped in <a> tags, but you can remove that line if you like:

    Using PHP highligher, but it's all JS:

    PHP Code:
    <script>
    function
    test()
    {
        for(var
    a = 0; a < 10; a++)
        {
            var
    elm = document.createElement("a");
            
            
    elm.id = a;
            
    elm.href = "javascript: void(0);";
            
    elm.innerHTML = " " + a + " ";
            
            
    document.body.appendChild(elm);
            
            
    document.getElementById(a).addEventListener("click", display_value, false);
        }
    }

    // Must pass in the event or pedantic Firefox won't like it.
    function display_value(event)
    {
        
    // MSIE supports srcElement, not target. Condition for both, browser decides.
        
    alert((event.srcElement || event.target).innerHTML);
    }

    </script>

    <body></body>
    This is a cross-browser solution and works in the top 5 (MSIE, FF, Chrome, Safari, Opera).
    spacer
      #3  
    spacer Yesterday, 11:33 AM
    faithless spacer
    Registered User
     
    Join Date: Nov 2011
    Posts: 19
    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>
    spacer
      #4  
    spacer Yesterday, 12:13 PM
    gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.