function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Andee Weir 17Andee Weir 17 

Set ui:inputcheckbox value in javascript

Hi & thanks for taking the time to look at my question,

I have a requirement to only allow system admins to uncheck a checkbox on a custom lightning component.  I've included example code below that replicates some of the behaviour but jI cannot work out how to set the value of the checkbox back to true if the alert is displayed.  Hopefully it's pretty obvious what I am trying to do from the code.  Just for your information the real component has the inputcheckbox inside a <aura:iteration/> so I don't think I can use anything like Document.getElementById().

Thanks again.

Component :-

<aura:component >
    <ui:inputCheckbox aura:id="childCheckbox" value="true" change="{!c.onChangeChildCheckbox}"/>Test
</aura:component>

Controller :-
({

    onChangeChildCheckbox : function(component, event, helper) {

        // If the checkbox was unchecked by the user...
        if(!event.getSource().get('v.value')){
            var profileName = "Basic User"; // This would normally be coming from the component class

            // If the user is not a system administrator, display alert & set the checkbox back to checked 
            if (profileName != "System Administrator"){
                alert("Not Allowed!");
                // PROBLEM - How to set the checkbox value to be checked. Code below doesn't work
                var temp = event.getSource();
                temp.value = true;
            }
        }
    }
})
Best Answer chosen by Andee Weir 17
Andee Weir 17Andee Weir 17
Hi Abhishek,

Thanks for taking the time to reply.

In the end it was quite simple.

Replaced :-

               // PROBLEM - How to set the checkbox value to be checked. Code below doesn't work
                var temp = event.getSource();
                temp.value = true;

With :-

               event.getSource().set("v.value", true);

Thanks again,

Andee

 

All Answers

Abhishek BansalAbhishek Bansal
Hi Andee,

Just try temp.checked = true

Thanks,
Abhishek Bansal.
Andee Weir 17Andee Weir 17
Hi Abhishek & thanks.

Afraid that doesn't work.  Get the following error :-

This page has an error. You might just need to refresh it.
Action failed: c:TestInputCheckbox$controller$onChangeChildCheckbox [Cannot add property checked, object is not extensible]
Failing descriptor: {c:TestInputCheckbox$controller$onChangeChildCheckbox}

Interestingly if I add an alert(temp); after initialising it it does come back with :-

SecureComponentRef: markup://ui:inputCheckbox {5:0} {childCheckbox}{ key: {"namespace":"c"} }

Any other ideas?
Abhishek BansalAbhishek Bansal

Hi Andee,

It looks like the property is not being applied to the checkbox itself and being applied to some other object that is not extensible. Since it is related to the javascript stuff so we need to debug the code in order to find the right solution.
It would be good if you can contact me on Gmail : abhibansal2790@gmail.com or Skype : abhishek.bansal2790.

Thanks,
Abhishek Bansal

Andee Weir 17Andee Weir 17
Hi Abhishek,

Thanks for taking the time to reply.

In the end it was quite simple.

Replaced :-

               // PROBLEM - How to set the checkbox value to be checked. Code below doesn't work
                var temp = event.getSource();
                temp.value = true;

With :-

               event.getSource().set("v.value", true);

Thanks again,

Andee

 
This was selected as the best answer