If you're using a normal input, you need to get the element and then check its checked attribute:
logChange: function(component, event, helper) {
console.log(component.find("someAuraId").getElement().checked);
}
If you're using ui:inputCheckbox
or lightning:input type="toggle"
, and you need to know the value, consider binding to an attribute. Here's both types together:
<aura:attribute name="box1" type="Boolean" default="true" />
<aura:attribute name="box2" type="Boolean" default="true" />
<aura:handler name="change" value="{!v.box1}" action="{!c.logChange1}" />
<aura:handler name="change" value="{!v.box2}" action="{!c.logChange2}" />
<ui:inputCheckbox value="{!v.box1}" label="The Box To Check" />
<lightning:input name="box2" type="toggle" checked="{!v.box2}" label="The Other Box To Check" />
logChange1: function(component, event, helper) {
console.log(component.get("v.box1"));
},
logChange2: function(component, event, helper) {
console.log(component.get("v.box2"));
},
First off, you're on the right track.
One of the odd things with web components is this new concept of the Shadow DOM. With the Shadow DOM, you cannot style base components with either css classes or the style attribute. (Read up on the Shadow DOM here: https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.create_dom)
This means you will not be able to style the lightning-button, unfortunately. However, there's nothing stopping you from using a regular old HTML button. With that in mind, it's time to head on over to the Lightning Design System site for a code snippet. https://www.lightningdesignsystem.com/components/buttons/ provides us a few examples, including a button with an icon.
Additionally, you're slightly over complicating this logic of toggling on and off the active piece of the button. You can achieve the same functionality with a simple boolean variable and two getter methods. I took a stab at what this would look like for you below.
Another great place for some insights on lightning web components is the Trailhead Sample Gallery, so be sure to check that out if you haven't already. You can find the gallery here: https://trailhead.salesforce.com/sample-gallery
Javascript:
import { LightningElement} from 'lwc';
export default class PcSelectButtonFramework extends LightningElement {
buttonClicked; //defaulted to false
// Handles click on the 'Show/hide content' button
handleToggleClick() {
this.buttonClicked = !this.buttonClicked; //set to true if false, false if true.
}
get cssClass(){
return this.buttonClicked ? 'slds-button slds-button_neutral active' : 'slds-button slds-button_neutral inactive';
}
get iconName(){
return this.buttonClicked ? 'utility:hide' : 'utility:preview';
}
}
Component:
<template>
<button class={cssClass}>
<lightning-icon icon-name={iconName} onclick={handleToggleClick}>Non Profit</lightning-icon>
</button>
</template>
Best Answer
Check this documentation link in LWC which has example of what you are asking for
Code to toggle is in the below js file