dynamics button names

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
jm16000
Posts: 4
Joined: April 15th, 2011, 1:31 am

Hello everybody!

I'm trying to generate buttons with a "for" loop but I have problems with the name :

I would like to have this...

Code: Select all

function THEONE(thisObj) {

function take(nb)
{
alert (nb);    
    }

 function take_buildUI(thisObj) {
 var myPanel = (thisObj instanceof Panel) ? thisObj : new Window("palette", "", [100, 100, 200, 400]);	  

//**************
     var bt_take1 = myPanel.add("button", [10,10,90,40], "Take A");
      var bt_take2 = myPanel.add("button", [10,50,90,80], "Take B");
      var bt_take3 = myPanel.add("button", [10,90,90,120], "Take C");
      var bt_take4 = myPanel.add("button", [10,130,90,160], "Take D");
      var bt_take5 = myPanel.add("button", [10,170,90,200], "Take E");
      var bt_take6 = myPanel.add("button", [10,210,90,240], "Take F");
      var bt_take7 = myPanel.add("button", [10,250,90,280], "Take G");
            
	 bt_take1.onClick = function () { take (1);  }; 
 	bt_take2.onClick = function () { take (2);  }; 
 	bt_take3.onClick = function () { take (3);  }; 
 	bt_take4.onClick = function () { take (4);  }; 
 	bt_take5.onClick = function () { take (5);  }; 
 	bt_take6.onClick = function () { take (6);  }; 
 	bt_take7.onClick = function () { take (7);  };
//**************


return myPanel;
   } 

   var LITTLEPal = take_buildUI(thisObj);
   if ((LITTLEPal != null) && (LITTLEPal instanceof Window)) {
      LITTLEPal.center();
      LITTLEPal.show();
      }
   }


   THEONE(this);

... in a shorter and more convenient way like this

Code: Select all

function THEONE(thisObj) {

function take(nb)
{
alert (nb);    
    }

 function take_buildUI(thisObj) {
 var myPanel = (thisObj instanceof Panel) ? thisObj : new Window("palette", "", [100, 100, 200, 400]); 

//**************
myArray = new Array("Take A", "Take B", "Take C", "Take D", "Take E", "Take F", "Take G");

for (var n = 0; n < myArray.length; n++) 
    {
     var bt_take = myPanel.add("button", [10, 10+40*n, 90, 40+40*n], myArray[n]);
     bt_take.onClick = function () { take (n); }
    }
//**************


return myPanel;
   } 

   var LITTLEPal = take_buildUI(thisObj);
   if ((LITTLEPal != null) && (LITTLEPal instanceof Window)) {
      LITTLEPal.center();
      LITTLEPal.show();
      }
   }


   THEONE(this);

But if i do that, all the buttons do the same... all do the action of the last button.

I tried to use variables for the buttons but nothing seems to work...
Any idea ?

Thanks a lot :)
jm16000
Posts: 4
Joined: April 15th, 2011, 1:31 am

if someone search the same thing, i found a solution...

with the eval function

this line

Code: Select all

bt_take.onClick = function () { take (n); }
works better in this way :

Code: Select all

eval("bt_take.onClick = function () { take ("+n+");  }");
Post Reply