0

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

7
  • where is the code, please share Jun 1, 2017 at 14:07
  • where do you want to read the value? in the controller? Jun 1, 2017 at 14:17
  • Yes, I want to read it in the controller.
    – Ant12345
    Jun 1, 2017 at 14:23
  • Please don't use images for your code. Simply copy-pasting text is preferable.
    – sfdcfox
    Jun 1, 2017 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, 2017 at 14:39

4 Answers 4

4

Try this: :)

var selected = event.target.checked; //true or false
1
2

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

var something = component.get("v.something__c");
7
  • This gives a value: undefined. There is also no change if it is checked or unchecked
    – Ant12345
    Jun 1, 2017 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, 2017 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
    – Ant12345
    Jun 1, 2017 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?
    – Ant12345
    Jun 1, 2017 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
    – Ant12345
    Jun 1, 2017 at 15:52
0

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?

0

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

You must log in to answer this question.

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