Clearing a selected RadioButton control in Flex

by Peter deHaan on January 6, 2008

in RadioButton, RadioButtonGroup

The following example shows how you can use ActionScript to clear a currently selected radio button by setting the RadioButtonGroup class’s selection property to null.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/01/06/clearing-a-selected-radiobutton-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;

            private function reset():void {
                answers.selection = null;
            }

            private function check():void {
                if (answers.selectedValue) {
                    Alert.show(answers.selectedValue.toString());
                } else {
                    Alert.show("unselected");
                }
            }
        ]]>
    </mx:Script>

    <mx:RadioButtonGroup id="answers" />

    <mx:Form backgroundColor="red">
        <mx:FormItem>
            <mx:Text id="question"
                    text="Question..." />
        </mx:FormItem>
        <mx:FormItem>
            <mx:RadioButton id="answer1"
                    group="{answers}"
                    label="Answer 1" />
        </mx:FormItem>
        <mx:FormItem>
            <mx:RadioButton id="answer2"
                    group="{answers}"
                    label="Answer 2" />
        </mx:FormItem>
        <mx:FormItem>
            <mx:RadioButton id="answer3"
                    group="{answers}"
                    label="Answer 3" />
        </mx:FormItem>
        <mx:FormItem>
            <mx:RadioButton id="answer4"
                    group="{answers}"
                    label="Answer 4" />
        </mx:FormItem>
        <mx:FormItem>
            <mx:RadioButton id="answer5"
                    group="{answers}"
                    label="Answer 5" />
        </mx:FormItem>
        <mx:FormItem direction="horizontal">
            <mx:Button label="Check" click="check();" />
            <mx:Button label="Reset" click="reset();" />
        </mx:FormItem>
    </mx:Form>

</mx:Application>

View source is enabled in the following example.

{ 8 comments… read them below or add one }

1 JK October 5, 2008 at 3:53 pm

Wow. Thanks for that simple clear method. I tried everything before finding this

Reply

2 Richard Scott October 26, 2008 at 2:11 am

Copied your page exactly as you’ve listed the code. Got the check to work but not the reset. Is there a setting in my browser that could prevent this code from working? Is there some other setting that could inhibit this from working?

Thanks.

Reply

3 peterd October 26, 2008 at 7:23 am

Richard Scott,

Which version/build of the Flex SDK are you using? The easiest way to find out is to look in the installed Flex SDK’s root folder for a flex-sdk-description.xml file.

Peter

Reply

4 Richard Scott October 27, 2008 at 7:14 am

Hi Peter,

I am working with Adobe Flex Builder 2 Built on Eclipse Version 2.0.123459. Thanks for the quick response.

Reply

5 peterd October 27, 2008 at 8:19 am

Richard Scott,

Unfortunately, I believe it was a bug in Flex 2 which got fixed in Flex 3. You may need to update to the newer version of the Flex SDK (or else extend the RadioButton and/or RadioButtonGroup classes and copy the relevant fixed code from Flex 3).

Peter

Reply

6 Ramon November 20, 2008 at 10:25 pm

Muchas gracias!!!!!!!!!!! muy simple y facil de entender.

Reply

7 Ramon November 21, 2008 at 10:16 pm

Nuevamente, Gracias! por tu ejemplo sencillo pero alto en contenido

Reply

8 Neethun September 9, 2009 at 12:03 am

greately helpful for fresher in flex.
thanks

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: