Created
September 7, 2020 09:34
-
-
Save pchittum/0d6737d0ceb0274acd0345b8c999650e to your computer and use it in GitHub Desktop.
LWC Playground: DOM manipulation with CSS versus if:true/false
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.contentHidden { | |
display: none; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div data-id="A">This is the div</div> | |
<lightning-button onclick={handleClickHide} label="Hide"></lightning-button> | |
<lightning-button onclick={handleClickShow} label="Show"></lightning-button> | |
<br> | |
<div if:true={show}>Toggle div</div> | |
<lightning-button label="Toggle" onclick={handleToggle}></lightning-button> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { LightningElement} from 'lwc'; | |
export default class App extends LightningElement { | |
show = true; | |
handleClickHide(){ | |
const selector = 'A'; | |
const theDiv = this.template.querySelector('[data-id="' +selector+ '"]'); | |
theDiv.className = 'contentHidden'; | |
} | |
handleClickShow(){ | |
const selector = 'A'; | |
const theDiv = this.template.querySelector('[data-id="' +selector+ '"]'); | |
theDiv.className = ''; | |
} | |
handleToggle(){ | |
this.show = !this.show; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment