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');
  • I believe you may just have the name of the event wrong. On the checkboxes, the event being sent is called change like this:

    <ui:inputCheckbox aura:id="checkbox" label="Select?" change="{!c.onCheck}"/>
         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.

    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