try it this way.... then you don't have the loop running in the background.
ActionScript Code:
/************************ prototypes ********************************/
// author: Justlar [+], Submitted: 06.17.01 3p
//http://proto.layer51.com/d.aspx?f=164
Number.prototype.Currency = function(uom) { // uom = Unit of Measure. Default set to US Dollars ($).
var temp = this.toString();
var dot = temp.indexOf(".", 0);
if(uom==null) { uom="$" }
if (dot<=0) {
var thedollar = temp;
var cents = "00";
} else {
var thedollar = temp.substr(0,dot);
var cents = temp.substr(dot+1,temp.length);
}
if (thedollar.length>3) {
var d_Len = thedollar.length;
var d_brk = d_Len/3;
if ((d_Len % 3)==0) {
var d_brk = (d_Len/3)-1;
}
for (var i=1; i<=d_brk; i++) {
var thedollar = thedollar.substr(0,(d_Len-(3*i)-(i-1))) + "," + thedollar.substr((d_Len-(3*i)-(i-1)),(3*i)+(i-1));
d_Len = thedollar.length;
}
}
dollars = thedollar;
if (cents.length>2) {
cents = Math.round(cents.substr(0,2) + "." + cents.substr(2,cents.length));
} else if (cents.length==1){
cents = cents + "0";
}
return uom + dollars + "." + cents;
}
// author: moock
//http://www.oreillynet.com/pub/a/javascript/2003/05/20/colinmoock.html
TextField.prototype.onKeyDown = function () {
if (Key.getCode() == Key.ENTER) {
changeTextToDollars();
//trace('enter key hit');
}
};
changeTextToDollars=function(){
var cur=Number(dollars_txt.text);
//trace(cur);
dollars_txt.text=cur.Currency();
};
Key.addListener(dollars_txt);
note. there was a bug in currency prototype, but it is fixed in this code. (it wasnt showing dollar values less than 100
i also added a textfield method written by colin moock. this allows the textfield to capture when the enter key was hit. so that the user can enter their dollar amount and hit enter, and the currency will format.
the keylistener allows the specified textfield to 'listen' for the any key to be hit.
you can also add a 'submit button' and have that call the changetexttodollars function also.
to get this code to work for you do the following:
copy and paste the above code into a new fla.
put an input box on the stage.
give the input box an instance name of 'dollars_txt'
you can also change it a bit so when the dollars_txt looses focus, the input is formatted. you may also want to put some error handling in there so that can catch when someone puts something other than a number in the input box.
it should work fine. it does for me.