Search code examples
actionscript-3apache-flexmxml

ActionScript 3.0 - reading each MXML element


If there is already a post for this, I'm sorry but I could not find it.

I have the following form (sample):

<mx:Label x="105.5" y="0" text="Cadastramento Basico" fontSize="22"/>
<mx:Text x="140" y="42" text="Nome:"/>
<mx:TextInput x="176" y="40" id="nome"/>
<mx:Text x="138" y="74" text="E-mail:"/>
<mx:TextInput x="177" y="72" id="email"/>
<mx:Text x="125" y="105" text="Telefone:"/>
<mx:TextInput x="177" y="103" restrict="012456789" id="telefone"/>

And I need to read each element in ActionScript to clean up every one after a user has clicked on a submit button. In jQuery we have the function elements.each(), I just need something like this.

How can I do this please?


Solution

    1. Please make sure you read the documentation before asking questions:

    2. You should wrap your form into a <mx:Form> tag:

      <mx:Form id="myForm" label="Cadastramento Basico">
          <mx:FormItem label="Nome:" >
              <<mx:TextInput id="nome"/>
          </mx:FormItem>
          <mx:FormItem label="E-mail:" >
              <<mx:TextInput id="mail"/>
          </mx:FormItem>            
          <mx:FormItem label="Telefone:" >
              <<mx:TextInput id="telefone" restrict="012456789"/>
          </mx:FormItem>
      </mx:Form>
      
    3. Then do it manually by running through all items of the form and check if they have the proper type and a valid property to be reset (add them to the code below if necessary).

      private function clearForm():void
      {
           for each(var o:* in loginForm.getChildren() ){
               if(o is FormItem){
                   var item:FormItem = o as FormItem;
                   for each(var o2:Object in item.getChildren()){
                       if(o2.hasOwnProperty("text")) {
                           o2.text = "";
                       }
                   }
                }
           }
      }