TanGuy,

Thanks, I think that first link helped a lot. I see what the guy is saying
(and I forgot that var's all get moved up to the highest scope - its sad
that I used to aide a web-dev class). 

So now, I have something that works in terms of passing around variables
that make sense:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <title>Javascript Dynamic Variables</title>
  <script type="text/javascript">
    
    function TestClass( strName ){
      this.Name = strName;
    }
    
    TestClass.prototype.AlertValue = function( anyValue ){
      alert( this.Name + " is alerting: " + anyValue );
    }
    
    
    function Init(){
      var objContentDiv = document.getElementById( "content" );
      var objA = null;
      var objTest = new TestClass( "Tester" );
            
      // This runs the for loop using a LOCAL scope for each iteration. 
      (function loop( intI ){
        var intX = intI;
      
        if (intI < 10){
        
          // Create new A element.
          objA = document.createElement( "a" );
          
          // Set A display properties.
          objA.style.display = "block";
          objA.style.backgroundColor = "#F8F8F8";
          objA.style.border = "1px solid #333333";
          objA.style.padding = "10px 10px 10px 10px";
          objA.style.color = "#333333";
          objA.style.marginBottom = "15px";
          
          // Create text for the link.
          objA.appendChild( document.createTextNode( "I should alert " +
intX + " when clicked" ) );
          
          // Set the href.
          objA.setAttribute( "href", "##" );
        
          // Set the onclick method.
          objA.onclick = function(){ objTest.AlertValue( intX ) };
          
          // Attach the A to the content.
          objContentDiv.appendChild( objA );
        
        
          loop(intI + 1);
        }        
      
      })(0);
      
    }
      
      
    // Is the tester class available here (just testing).
    alert( "objTest is of type: " + typeof(objTest));
      
  </script>
</head>
<body onload="Init();">

  <div id="content"></div>

</body>
</html> 



Thanks for everyone's help. I think I now see the local scoping issue that I
was dealing with before. I will try to return with a better example.

.......................
Ben Nadel 
www.bennadel.com

-----Original Message-----
From: Tanguy Rademakers [mailto:[EMAIL PROTECTED] 
Sent: Thursday, April 20, 2006 1:19 PM
To: CF-Talk
Subject: Re: OT: Javascript help

Hi Ben,

I ran into this problem recently - these really helped me out:

http://joust.kano.net/weblog/archive/2005/08/08/a-huge-gotcha-with-javascrip
t-closures

and

http://jibbering.com/faq/faq_notes/closures.html

/t



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Message: http://www.houseoffusion.com/lists.cfm/link=i:4:238316
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54

Reply via email to