-
Notifications
You must be signed in to change notification settings - Fork 0
/
lifecycle-wc.js
47 lines (45 loc) · 1.42 KB
/
lifecycle-wc.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class AttributeChangedWC extends HTMLElement {
// required for
static get observedAttributes() {
return ["add"];
}
connectedCallback() {
this.add = this.getAttribute("add");
this.innerHTML = `
<style>#sum-title{ color: blue;} .sum { color: black; font-weight: bold; }</style>
<div>
<p id="old-value">Old Attr Value: <span class="old">0</span></p>
<p id="new-value">New Attr Value: <span class="new">0</span></p>
</div>
<p id="sum-title">Sum: <span class="sum">0</span></p>
`;
}
attributeChangedCallback(attrName, oldValue, newValue) {
if (attrName === "add") {
console.log(`Old add Value: ${oldValue}, New add Value: ${newValue}`);
let sum = 1;
const sumDisplay = this.querySelector(".sum");
const oldVal = this.querySelector(".old");
const newVal = this.querySelector(".new");
console.log("sumDisplay: ", sumDisplay);
if (oldValue) {
oldVal.innerHTML = oldValue;
newVal.innerHTML = newValue;
sum = parseInt(oldValue) + parseInt(newValue);
} else {
sum = parseInt(newValue);
}
console.log("sum: ", sum);
if (sum !== null && sumDisplay !== null) {
sumDisplay.innerHTML = sum;
}
}
}
get add() {
return this.getAttribute("add");
}
set add(val) {
this.setAttribute("add", val);
}
}
customElements.define("attribute-changed-wc", AttributeChangedWC);