# How to create a button from array of buttons?

**URL:** <https://forum.kirupa.com/t/how-to-create-a-button-from-array-of-buttons/636750>\
**Category:** flash\
**Created:** [June 11, 2017, 10:20pm UTC](https://forum.kirupa.com/t/how-to-create-a-button-from-array-of-buttons/636750 "2017-06-11T22:20:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Serifaz](https://avatars.discourse-cdn.com/v4/letter/s/59ef9b/32.png) [@Serifaz](https://forum.kirupa.com/u/Serifaz)\
**Post date:** [June 11, 2017, 10:20pm UTC](https://forum.kirupa.com/t/how-to-create-a-button-from-array-of-buttons/636750/1 "2017-06-11T22:20:44Z")

</div>

Hello I am trying to create an array of buttons that I can click and then trace out the name of those buttons  
This is what I have.

```
	  private function charList(event:ProgressEvent):void {
   	 while(socket.bytesAvailable)
		{
		 		var str:String = this.socket.readUTFBytes(this.socket.bytesAvailable);
				var numbers:String = str.split("\"").join("'");
				// post my stat values :)
				var val:Array = str.split("+|+");
				var numberValues:Array=numbers.match(/(\d+)/ig);
				if(val[0].toString().length == 0)
				{
					val[0] = "create";
				}
				Names = [val[0], val[5], val[10], val[15], val[20], val[25], val[30], val[35], val[40], val[45]];
				for(var i:int = 0; i < Names.length; i++) {
					
						
						cSel.chrNam.text = Names[i];
						cSel.buttonMode = true;
						cSel.x = 100;
						cSel.y = 100 + i * 60;
						addChild(cSel);
						var e:MouseEvent;
						cSel.addEventListener(MouseEvent.CLICK, selectCharacter);
						
						
				}
			}
								
		 
	}

```

how would I trace out the Names[i] value when triggering the selectCharacter function?

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [June 13, 2017, 4:07pm UTC](https://forum.kirupa.com/t/how-to-create-a-button-from-array-of-buttons/636750/2 "2017-06-13T16:07:19Z")

</div>

What is `cSel`? If you’re adding it in the loop, it should get created in that loop too. Something like

```auto
var cSel = new Button() // or whatever

```

At that point, if you want to get `Names[i]` out of the click handler, in `selectCharacter` you’d use

```auto
trace(event.currentTarget.chrNam.text)

```

assuming you’re first parameter in `selectCharacter` is named `event`. This gets `Names[i]` because this is where you stored it for the button in the loop.

---

<div class="post-metadata">

**Author:** ![dynastyteam](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/dynastyteam/32/8429_2.png) [@dynastyteam](https://forum.kirupa.com/u/dynastyteam)\
**Post date:** [June 15, 2017, 5:26am UTC](https://forum.kirupa.com/t/how-to-create-a-button-from-array-of-buttons/636750/3 "2017-06-15T05:26:33Z")

</div>

thanks senocular
