Question

I would like a way to capture text from multiple input fields and then display images next to the text field based on what text is in the text field.

So imagine a user types into the text fields:

tomatoes cabbage chocolate

an image appears next to the tomatoes field another image appears next to the cabbage field etc.

I have the text fields, I have a case statement, but I don't know how to combine the components together or how to display the image.

Was it helpful?

Solution

It depends on what you're wanting to do with it.

It sounds like you're wanting an image of tomatoes to be next to the tomatoes text field, an image of cabbage next tothe cabbage one, and likewise for chocolate. At first the user can't see any of the images, but when the user types anything at all in one of those text fields, the image next to it suddenly shows up. Is this so?

If that's the case, then one of the simplest (though not the most efficient) ways to do this is to go ahead and add those images next to the text fields normally, just as though they show up all throughout the program. Go ahead and position them all right where they're going to go, size them, everything. The only difference here is that you want to set each one of their visible properties to false.

Then you could either have a separate function handling each text field's event listener, or you could have that switch statement you're talking about in just one function that handles all three of those text fields. Then:

private function onChange(event:Event):void
{
    if (event.target.text != "")
    {
        switch (event.target)
        {
            case txtTomatoes:
                imgTomatoes.visible = true;
                break;

            case txtCabbage:
                imgCabbage.visible = true;
                break;

            case txtChocolate:
                imgChocolate.visible = true;
        }
    }
}

This is only one way to do this, and it depends on where you're wanting to go with it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top