I can't figure out how to read out the value from a checkbox. See my code below. Is there someone who can give me some tips?

See my code

  • where is the code, please share – Nitish Singhal Jun 1 '17 at 14:07
  • where do you want to read the value? in the controller? – Nitish Singhal Jun 1 '17 at 14:17
  • Yes, I want to read it in the controller. – Ant Jun 1 '17 at 14:23
  • Please don't use images for your code. Simply copy-pasting text is preferable. – sfdcfox Jun 1 '17 at 14:39
  • Is this a lightning component? With just an input tag you need to get the element by Id and then the value. If you can use apex or lightning input component it would be easier – Eric Jun 1 '17 at 14:39

To get the value, just get the value from the attribute you assigned:

var something = component.get("v.something__c");
  • This gives a value: undefined. There is also no change if it is checked or unchecked – Ant Jun 1 '17 at 15:13
  • @Ant We'll need to see more code. Preferably enough to reproduce the issue. I can say that, generically speaking, this should work. I've done it enough times to know that this is how it should work. – sfdcfox Jun 1 '17 at 15:40
  • Thanks for helping me, more code is not a problem. The actual code in the component is this: <aura:attribute name="identityRequest" type="Identity_Request__c" description="Identity request record"/> <div class="slds-form-element__row"> <div class="slds-form-element"> <label class="slds-checkbox"> <input type="checkbox" aura:id="news" class="slds-input" value="{!v.identityRequest.Newsletter__c}" onchange="{!c.onChange}" /> <span class="slds-checkbox--faux"></span> </label> </div> </div> And the code in the controller: see next comment – Ant Jun 1 '17 at 15:48
  • onChange : function(component, event, helper) { var identityRequest = component.get('v.identityRequest'); component.set('v.identityRequest',identityRequest); console.log('identityRequest..Newsletter__c); } As you can see Im pretty new to this, is this readable? – Ant Jun 1 '17 at 15:50
  • Btw I can read out: <lightning:input type="email" aura:id="email" value="{!v.identityRequest.Email__c}" class="slds-lookup__search-input slds-input--bare slds-has-divider--bottom" name="email" label="{!v.labels.email__c+' *'}" onchange="{!c.onChange}"/> In the controller using the same controller code. So Im guessing im missing a step because its a checkbox – Ant Jun 1 '17 at 15:52

Use something as :

<ui:inputCheckbox aura:id="checkbox" label="Select?" change="{!c.onCheck}"/>

in component and

onCheck: function(cmp, evt) {
     var checkCmp = cmp.find("checkbox");
     resultCmp = cmp.find("checkResult");
     resultCmp.set("v.value", ""+checkCmp.get("v.value"));


in controller. This should return the value is checked or not?


In case when normal html input tag is used, event.target.checked will return if the checkbox has just been checked or not. event.target.value will return the 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.