3

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)
}

4 Answers 4

4

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

1
  • 1
    Thanks, it was indeed the wrong name of the event. change was correct.
    – Ant12345
    Commented Jun 6, 2017 at 15:11
1

change <input type="checkbox"/> to <ui:inputCheckbox /> and use the following code

        onChange : function(component, event, helper) {
            var identityRequest = component.find("news").get("v.value");
            component.set('v.identityRequest',identityRequest);
            console.log(identityRequest)
        }
1
  • Thanks for the response, but the ui:inputCheckbox doesn't handle the onchange event listener. What could I use to replace this? Im pretty new to this.
    – Ant12345
    Commented Jun 6, 2017 at 13:50
0

You can try with the below line of codes.

< input type="checkbox" id="news" value= "news" checked={!v.identityRequest.Newsletter__c}" onchange="{!c.onChange}" />

onChange: function(component, event, helper) {
    var check = document.getElementById("news").checked;
    alert('check ::'+check );
    //Rest of the codes...
}
-2

You can you use lightning: input with the checked attribute for dynamic binding

<lightning:input type="checkbox" name= 'isMaster' checked="{!record.IsMaster}"></lightning:input>

1
  • why is this downvoted so much? it's the correct answer.
    – ViqMontana
    Commented Apr 4, 2023 at 9:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .