How do I read the value from an input checkbox?

  • I am not managing to read out the value of a checkbox in my lightning component. Please find my code below:

    lightning component:

    < label class="slds-checkbox">
            < input type="checkbox" aura:id="news" class="slds-input" checked="{!v.identityRequest.Newsletter__c}" onchange="{!c.onChange}" />
    < span class="slds-checkbox--faux"></span>< /label>
    

    apex controller:

    onChange : function(component, event, helper) {
        var identityRequest     = component.get('v.identityRequest');
        component.set('v.identityRequest',identityRequest);
        console.log(identityRequest)
    }
    
  • I believe you may just have the name of the event wrong. On the checkboxes, the event being sent is called change like this:

    component.cmp
    <ui:inputCheckbox aura:id="checkbox" label="Select?" change="{!c.onCheck}"/>
    
    
    
    controller.js
    ({
         onCheck: function(cmp, evt) {
             var checkCmp = cmp.find("checkbox");
             resultCmp = cmp.find("checkResult");
             resultCmp.set("v.value", ""+checkCmp.get("v.value"));
    
        }
    })
    

    Keep me posted if that solved your issue. I went by the documentation and have not tested it myself.

    https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/aura_compref_ui_inputCheckbox.htm

    Thanks, it was indeed the wrong name of the event. change was correct.

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM