I'm trying to change the icon of a lightning button icon but the image isn't changing however I see that the attribute is being updated.
this.template.querySelector('#'+event.target.id).setAttribute("icon-name","utility:check");
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.
Sign up to join this communitysetAttribute is for setting HTML attributes. For component attributes, use direct assignment:
event.target.iconName='utility:check"
If you use query selectors, just remember that if it's not an HTML component, use the direct assignment method.
Using track variable is another way to go.
In the javascript controller
@track iconname = 'DefaultIconName';
handleToggleIconname(){
this.iconname = 'NewIconName';
}
In the html template file
<lightning-icon icon-name={iconname}></lightning-icon>
@track
is no longer necessary for shallow references, such as a string variable in your answer. You only need to track objects and arrays if you want them to be reactive.