Skip to main content
Participant
November 26, 2010
Answered

Arrays with textboxes

  • November 26, 2010
  • 1 reply
  • 517 views

Hi I am new to flash and trying to output numbers I have put into textboxes with a button.

My code so far:

var myArray: Array = new Array();


myArray.push(Number(textbox1.text))
myArray.push(Number(textbox2.text))
myArray.push(Number(textbox3.text))

button1.addEventListener(
  MouseEvent.MOUSE_UP,
  function(evt:MouseEvent):void {
    trace(myArray);
  }
);

So I have made 3 textboxes and a button, I run this, and when I input info into these textboxes and hit the button it displays 0,0,0 like it doesn't read the numbers i have inputted into the textbox.

Any thoughts?

thank you in advance x

This topic has been closed for replies.
Correct answer Ned Murphy

An array will be fine if you need someplace to store the values, but you need to wait until the textboxes have values.  You can build that into your button code, assuming the button gets clicked after the entries are made...

var myArray: Array = new Array(); 
  
function mUp(evt:MouseEvent):void {

    myArray.push(Number(textbox1.text));
    myArray.push(Number(textbox2.text));
    myArray.push(Number(textbox3.text));
    trace(myArray);
}

button1.addEventListener( MouseEvent.MOUSE_UP, mUp);

Another way you could do it is to add event listeners to the extfields.  If you add an Event.CHANGE listener you can have the same function used for all three textfields...

var myArray: Array; 

function addToArray(evt:Event):void {

    myArray = [];

    myArray.push(Number(textbox1.text));
    myArray.push(Number(textbox2.text));
    myArray.push(Number(textbox3.text));

}

textbox1.addEventListener(Event.CHANGE, addToArray);

textbox2.addEventListener(Event.CHANGE, addToArray);

textbox3.addEventListener(Event.CHANGE, addToArray);

function mUp(evt:MouseEvent):void {

    trace(myArray);
}

button1.addEventListener( MouseEvent.MOUSE_UP, mUp);

1 reply

Ned Murphy
Legend
November 26, 2010

Do you have values assigned to the textboxes before that push code executes?

You should avoid nesting functions within the event listener...

var myArray: Array = new Array();

 

// values need to be in the textfields before these next 3 lines execute

 
myArray.push(Number(textbox1.text))
myArray.push(Number(textbox2.text))
myArray.push(Number(textbox3.text))

 
function mUp(evt:MouseEvent):void {
    trace(myArray);
}

button1.addEventListener( MouseEvent.MOUSE_UP, mUp);

BoneStarrAuthor
Participant
November 26, 2010

Well when I put values into the textfields thats when It works and it will trace those numbers, but what i'm trying to do is get the user to input these numbers, So basically i need a way of putting user inputs into an array or maybe not an array if it can be done a different way where i can call these inputs later on, i thought an array would be easiset.

Ned Murphy
Ned MurphyCorrect answer
Legend
November 27, 2010

An array will be fine if you need someplace to store the values, but you need to wait until the textboxes have values.  You can build that into your button code, assuming the button gets clicked after the entries are made...

var myArray: Array = new Array(); 
  
function mUp(evt:MouseEvent):void {

    myArray.push(Number(textbox1.text));
    myArray.push(Number(textbox2.text));
    myArray.push(Number(textbox3.text));
    trace(myArray);
}

button1.addEventListener( MouseEvent.MOUSE_UP, mUp);

Another way you could do it is to add event listeners to the extfields.  If you add an Event.CHANGE listener you can have the same function used for all three textfields...

var myArray: Array; 

function addToArray(evt:Event):void {

    myArray = [];

    myArray.push(Number(textbox1.text));
    myArray.push(Number(textbox2.text));
    myArray.push(Number(textbox3.text));

}

textbox1.addEventListener(Event.CHANGE, addToArray);

textbox2.addEventListener(Event.CHANGE, addToArray);

textbox3.addEventListener(Event.CHANGE, addToArray);

function mUp(evt:MouseEvent):void {

    trace(myArray);
}

button1.addEventListener( MouseEvent.MOUSE_UP, mUp);