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
Mayank_SharmaMayank_Sharma 

i need to uncheck the checkbox if second checkbox is clicked and vice-versa . I need to implement this in Lightning.Kindly suggest how to do thjis

Best Answer chosen by Mayank_Sharma
Piyush Kumar 58Piyush Kumar 58
Hello Myank 

Try this code for your requirment . I have wirte this code in my org and its work fine for uncheck the checkbox if second checkbox is clicked and vice-versa.

component:-
<aura:component >
		<ui:inputCheckbox aura:id="checkbox" label="Select?" change="{!c.onCheck1}"/>
        <ui:inputCheckbox aura:id="checkbox1" label="secondOneSelect?" change="{!c.onCheck2}"/>	
</aura:component>
controller:-
({
	onCheck1: function(cmp, evt) {
         var checkCmp1 =cmp.find("checkbox").get("v.value");
         var checkCmp2 =cmp.find("checkbox1").get("v.value");
         if(checkCmp1){
           var temp =cmp.find("checkbox1").set("v.value", false);
        }
         
	},
    onCheck2: function(cmp, evt) {
         var checkCmp1 =cmp.find("checkbox").get("v.value");
         var checkCmp2 =cmp.find("checkbox1").get("v.value");
         if(checkCmp1){
           var temp =cmp.find("checkbox").set("v.value", false);
        }
         
	}
})

Thanks 
Piyush Kumar

 

All Answers

Piyush Kumar 58Piyush Kumar 58
Hello Myank 

Try this code for your requirment . I have wirte this code in my org and its work fine for uncheck the checkbox if second checkbox is clicked and vice-versa.

component:-
<aura:component >
		<ui:inputCheckbox aura:id="checkbox" label="Select?" change="{!c.onCheck1}"/>
        <ui:inputCheckbox aura:id="checkbox1" label="secondOneSelect?" change="{!c.onCheck2}"/>	
</aura:component>
controller:-
({
	onCheck1: function(cmp, evt) {
         var checkCmp1 =cmp.find("checkbox").get("v.value");
         var checkCmp2 =cmp.find("checkbox1").get("v.value");
         if(checkCmp1){
           var temp =cmp.find("checkbox1").set("v.value", false);
        }
         
	},
    onCheck2: function(cmp, evt) {
         var checkCmp1 =cmp.find("checkbox").get("v.value");
         var checkCmp2 =cmp.find("checkbox1").get("v.value");
         if(checkCmp1){
           var temp =cmp.find("checkbox").set("v.value", false);
        }
         
	}
})

Thanks 
Piyush Kumar

 
This was selected as the best answer
Mayank_SharmaMayank_Sharma
Thanks Piyush. I also implemented the same yesterday. :)
Piyush Kumar 58Piyush Kumar 58
Wlcome Mayank..
Mohammad AnisMohammad Anis
Hi Mayank,

The below solution worked for me and this is w.r.t. the any numbers of checkboxes as:
Component Code:

  <aura:attribute name="offering" type="List" />

<aura:iteration items="{!v.offering}" var="offeringval"> 
<th scope="row"><div class="slds-truncate" ><ui:inputRadio name="checkThis" class="slds-radio" text="{!offeringval.offering.Id}" change="{!c.onRadio}"/></div></th>
 </aura:iteration>

Controller Code:
onRadio: function(component, event) {
        var selected = event.getSource().get("v.text");
        var cbs = document.getElementsByClassName("slds-radio");
            for (var i = 0; i < cbs.length; i++) {
            cbs[i].checked = false;
            }
        component.set("v.checkThis", true);
        component.set("v.selectedRecordId", selected);

        alert('hello'+component.get("v.selectedRecordId"));
     }

Thanks!