Search:

Levels of Experience

Beginner

Intermediate

Advanced




Related Examples

Accordian with Change Event - Intermediate

MXML Accordian component

Form Validation Example - Advanced

This is a form validation example that checks for text validation. If there is an error, it changes ...
Click for more

Simple Click Event

This is a simple click event that shows how to add an event listener and how to pass in the button object into a function and alert its property.

Creating a button using mxml and giving it a label and an id.

<mx:Button label="Click Me" id="myBtn" />

Creating an init function for the creationComplete event on the Application tag. This will run when everything has loaded. This equivalent to a event in HTML. I then give it a private function called 'myFunction'. I pass in the button object when clicked, and alert out its label text.

<mx:Script>
    	<![CDATA[
    	
    		import mx.controls.Alert;
    		
    		//This is my initialize function from my creationComplete event on my Application Tag.
    		private function init():void{
    			//Creating an event listener for my button.
    			myBtn.addEventListener(MouseEvent.CLICK,myFunction);
    		}
    		
    		private function myFunction(evt:MouseEvent):void{
    			//evt is the button and its properties getting passed into the function.
    			//I am going to Alert the buttons label value. I could also do Alert.show('Hello World');    			
    			Alert.show(evt.currentTarget.label);
    		}
    	]]>
    </mx:Script>