-
Notifications
You must be signed in to change notification settings - Fork 0
/
display-toggle.js
26 lines (26 loc) · 972 Bytes
/
display-toggle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class SimpleToggle extends HTMLElement {
constructor() {
super();
this.button = document.createElement('button');
this.button.textContent = 'Show';
this.button.className = 'showHideButton';
this.button.onclick = () => this.toggleContent();
this.content = document.createElement('div');
this.content.style.display = 'none';
this.template = document.createElement('template');
this.template.innerHTML = this.getAttribute('c');
this.append(this.button, this.content);
}
toggleContent() {
if (this.content.style.display === 'none') {
this.content.appendChild(this.template.content.cloneNode(true));
this.content.style.display = 'block';
this.button.textContent = 'Hide';
} else {
this.content.innerHTML = '';
this.content.style.display = 'none';
this.button.textContent = 'Show';
}
}
}
customElements.define('s-t', SimpleToggle);