2

I can't get value selected on lightning:radioGroup. It return undefined

<lightning:radioGroup aura:id="test"name="testName" options="{! v.options }" required="true" onchange="{! c.handleChangeButtonGroup }"/>

Controller

handleChangeButtonGroup:function(cmp, event, helper){
    var auraIdField = event.getSource().getLocalId();
    console.log(cmp.find(auraIdField).get("v.value"));
},

I don't want using event.getParam("value");, just using component get value it

2 Answers 2

7

Try this

Component:

<aura:component>
    <aura:attribute name="options" type="List" default="[
        {'label': 'Option1', 'value': '1'},
        {'label': 'Option2', 'value': '2'}
    ]"/>

    <aura:attribute name="radioGrpValue" type="String" />

    <lightning:radioGroup
        aura:id="radioGrp"
        name="radioButtonGroup"
        label="Choose Your Option"
        options="{! v.options }"
        value="{! v.radioGrpValue }"
        onchange="{! c.handleChange }"
    />
</aura:component>

Controller:

handleChange : function(component, event, helper) {
    var radioGrpValue = component.get("v.radioGrpValue");
    console.log('radioGrpValue',radioGrpValue);
}
0
3

Add value attribute to the radioGroup component and bind it some attribute or sObject field.

<aura:component>
    <aura:attribute name="value" type="string" default=""/>
    <aura:attribute name="options" type="string[]" default="[
    {'label': 'apples', 'value': 'option1'},
    {'label': 'oranges', 'value': 'option2'}
    ]"/>

    <lightning:radioGroup aura:id="test" name="testName" options="{! v.options }" value="{!v.value}" required="true" onchange="{! c.handleChangeButtonGroup }"/> 
</aura:component>

js Controller

({
    handleChangeButtonGroup:function(cmp, event, helper){
        var auraIdField = event.getSource().getLocalId();
        console.log(auraIdField);
        console.log(cmp.find(auraIdField).get("v.value"));
    },
})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.