Home > Adobe Flex > Flex: How to execute a method from an htmlText anchor

Flex: How to execute a method from an htmlText anchor

With Flex, I learn something new every day.

Today I wanted to add a link to the htmlText property of an mx:Text component. However, I wanted the link to actually execute a function rather than navigate to some external content.

When I’ve needed this in the past, I’ve had to very carefully layout some Text components inline with a LinkButton to give the appearance of a homogeneous flow of text, but this was clunky and I figured there had to be a better way.

The mx:Text component has a built-in event listener property called “link”:

<mx:Text link="onLinkClick(event)"/>

You can dispatch link events from your anchor href tags embedded in your htmlText. The event type can be passed through the listener to the handler to accommodate multiple event types:

<mx:Text link="onLinkClick(event)">
			This is my HTML.  I can <a href="event:myEventType">link</a> this text to execute an actionscript method.

The handler can then use the text property of the TextEvent to use in a switch or other conditional if your htmlText contains multiple links, or you can skip passing the event to the handler altogether if you wish.

private function onLinkClick( event:TextEvent ) : void
	if ( event.text == "myEventType" )
		//do something

This is incredibly simple to implement and would have come in sooo handy in the past.

C’est la vie ;)

Categories: Adobe Flex Tags:
  1. January 9th, 2014 at 10:07 | #1

    Touche. Sound statuetki justifications. Keep up the
    great spirit.

    This kind of information etui na okulary is worth everyone’s attention.
    When will I read more?

  2. April 2nd, 2014 at 17:01 | #2

    Wow that was strange. I just wrote an incredibly long comment but after I clicked submit my
    comment didn’t appear. Grrrr… well I’m not writing all that
    over again. Regardless, just wanted to say wonderful

  3. May 8th, 2014 at 03:58 | #3

    ” because the calories are far below the American College of Sports Medicine’s (ACSM) recommended daily caloric intake of 1,200 calories for women, and 1,800 calories for men. So generally, eat meat, fish, fruit and vegetables. That doesn’t mean you can go back to eating fast food every night but you won’t have to stick to 500 calories. What’s more, HCG diet plan strategy plan prohibits citizens from decreasing the cost on utilization of everyday use. In doing so, the weight lost comes directly from unhealthy fat and does not strip the body of much needed muscle, vitamins or minerals essential to maintain good health, while at the same time, releasing excessive amounts of fat-stored nutrients into the blood stream to be absorbed by the body.

  4. May 24th, 2014 at 13:39 | #4

    ‘ Propecia is meant to be used in men suffering from male pattern baldness after consulting a doctor.

    A study published in The Journal of Dermatology revealed that chlorine causes discolouration and a change in hair texture.
    She did not seem too enthusiastic about it and claimed that
    she had just been to the toilet and did not feel like peeing any more.

  5. May 27th, 2014 at 03:20 | #5

    In a few cases, the worms can literally eat the food that you’re consuming, stripping your body of the nutrients it needs.
    One of the disadvantages of low-calorie diets is that it makes you feel constantly hungry.
    Obviously if we have pain or anxiety about a tumor
    or cyst often times that.

  6. June 24th, 2014 at 19:00 | #6

    We are a bunch of volunteers and opening a brand new scheme in our community.

    Your web site provided us with helpful info to work
    on. You have performed a formidable task and our whole community
    shall be grateful to you.

  1. No trackbacks yet.