# How to create buttons dynamically

**URL:** <https://forum.kirupa.com/t/how-to-create-buttons-dynamically/6773>\
**Category:** Uncategorized\
**Created:** [October 16, 2002, 8:54am UTC](https://forum.kirupa.com/t/how-to-create-buttons-dynamically/6773 "2002-10-16T08:54:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kamelus](https://avatars.discourse-cdn.com/v4/letter/k/3ab097/32.png) [@kamelus](https://forum.kirupa.com/u/kamelus)\
**Post date:** [October 16, 2002, 8:54am UTC](https://forum.kirupa.com/t/how-to-create-buttons-dynamically/6773/1 "2002-10-16T08:54:27Z")

</div>

here is my contribution with this script you can create buttons dynamically and remove them  
(howmany) is the variable of the number of the buttons to create.

```auto
function makebuttons(){
	// if we have buttons created at first time we remove them to recreate others ones

	if(_global.createdClips.length>0){	
		clearbuttons();	
		}
		addbuttons(homany);
}

function addbuttons() {
// we create an array to save the buttons references
_global.createdClips = new Array(); 

for(i=1;i<=howmany;i++){
myButton = this.createEmptyMovieCli("but"+i,i+_global.maxSeed)
          myButton.lineStyle(1);
        myButton.beginFill(0x336699);
        myButton.lineTo(22,0);
        myButton.lineTo(22,20);
        myButton.lineTo(0,20);
        myButton.lineTo(0,0);
        this["but"+i]._x = i*myButton._width*1;
		myButton.createTextField("txt"+i,i+10);
		myButton["but"+i].text=i;

// when the movieclip is created we save the reference in the array using this syntax

_global.createdClips.push(myButton); 

//=====================we create another movie to make a 3Doutline to the buttonD========================

myButton = this.createEmptyMovieClip("contour"+i,i+_global.maxSeed+20);
        myButton.lineStyle(1.75,0x999999);
    	myButton.lineTo(22,0);
        myButton.lineTo(22,20);
        myButton.lineStyle(1.75,0xffffff);
       myButton.lineTo(0,20);
        myButton.lineTo(0,0);
this["contour"+i]._x = i*myButton._width*1;

_global.createdClips.push(myButton); 

//=============we create a textfield to store the caption ====================================
myButton.createTextField("text"+i, 0, 3, 1, 18, 20); 
myButton["text"+i].multiline = false; 
myButton["text"+i].wordWrap = false; 
myButton["text"+i].border = false; 
myButton["text"+i].background = false; 
myButton["text"+i].variable = ""; 
myformat = new TextFormat(); 
myformat.font="verdana";
myformat.size="9";
myformat.color = 0xffffff; 
myformat.bullet = false; 
myformat.underline = false; 
myButton["text"+i].text = i; 
myButton["text"+i].setTextFormat(myformat); 
_global.createdClips.push(myButton); 

// we attach event to the button
 myButton.onPress = function(){

          //you place the code you want 
 				
        }
 
myButton.onRelease = function(){

 // you place the code you want
		
   }
 myButton.onRollOver = function(){

// you place the code you want
        }

myButton.onRollOut = function(){

// you place the code you want
       }

}

}

function clearbuttons() {	
for(var myButton in _global.createdClips){ 	
_global.createdClips[myButton].removeMovieClip();

// we delete the reference of the movieclip removed from the array

_global.createdClips.pop();

}
}

```

that’s all  
i will try to ameliorate this code

enjoy it  
if you have any question post your comment

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 16, 2002, 10:53am UTC](https://forum.kirupa.com/t/how-to-create-buttons-dynamically/6773/2 "2002-10-16T10:53:14Z")

</div>

Looks good, Kamelus. I’ll look at it in details later. First thing I noticed: shouldn’t you declare function addbuttons(num)?

pom :q:

Somebody rate this thread please. 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 16, 2002, 2:52pm UTC](https://forum.kirupa.com/t/how-to-create-buttons-dynamically/6773/3 "2002-10-16T14:52:51Z")

</div>

thank you ily

you 're right i have forgotten to declare the function addbuttons like this

function addbuttons(howmany){

}

instead

function addbuttons(){

}

thanks
