0

I make a textfield named 'nama' and button 'ayo'. I want to make the button disable when the textfield is empty. But when I try this code my button not disable and still working. I want the button disable before the textfield filling.

stop();
menumulaikuis();
var namasiswa:String;
var nama:TextField = new TextField();
namasiswa = nama.text;

nama.addEventListener(Event.CHANGE,handler);
function handler(event:Event){
    if (nama.text) {
        ayo.enabled = true;
        ayo.visible = true;
    } else {
        ayo.enabled = false;
        ayo.visible = false;
    }
}
akmozo
  • 9,829
  • 3
  • 28
  • 44
ayu
  • 23
  • 4

2 Answers2

1

You have some little problems in your code :

  • You should add your text filed to the stage using addChild() :
    var nama:TextField = new TextField();
    addChild(nama);
  • If your text field is for user's input, so its type should be input :
    nama.type = 'input';
  • To verify if a text field's text is empty, you can simply do :
    if(nama.text == ''){ /* ... */ }

So your text field's change handler can be like this :

function changeHandler(event:Event): void 
{
    if (nama.text != '') {
        ayo.enabled = true;
        ayo.visible = true;
    } else {
        ayo.enabled = false;
        ayo.visible = false;
    }
}

Hope that can help.

akmozo
  • 9,829
  • 3
  • 28
  • 44
0

If the Text Field isn't for user input, you cannot use the Event.CHANGE listener.

I suggest changing Event.CHANGE to Event.ENTER_FRAME; that should fix your problem.

  • That's wrong. The [`TextField`'s `Event.CHANGE`](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/TextField.html#event:change) event is fired with any `TextField` object even with a dynamic one. – akmozo Dec 18 '15 at 14:14
  • I see, I was referencing: http://stackoverflow.com/questions/977847/as3-textbox-change-event-not-firing – Naomi Dennis Dec 18 '15 at 19:52