newbie here. Thanks for your helps.
I'm wondering can I change if ...else ... to ? : statement in following codes:
https://codepen.io/lgtits/pen/MWoqPBX?editors=1010
list.addEventListener('click', function (event) {
let target = event.target
if (target.classList.contains('delete')) {
let parentElement = target.parentElement
parentElement.remove()
} else if (target.tagName === 'LABEL') {
target.classList.toggle('checked')
}
})
just like this:
let a = 1
let b = 2
if(a>b) {
console.log('bigger')
} else if (a === b) {
console.log('equal')
} else {
console.log('smaller')
}
a > b ? console.log('bigger') :
a === b ? console.log('equal') :
console.log('smaller')
You could change the first block if...else if to the following:
list.addEventListener("click", function (event) {
let target = event.target;
// set this ahead of time if needed
let parentElement = target.parentElement;
target.classList.contains("delete")
? parentElement.remove()
: target.tagName === "LABEL"
? target.classList.toggle("checked")
: void 0;
});
It can tend to get clunky if you have too many different things to check for, but it's doable if you're only doing one operation each in if and else blocks. Just remember to make sure it is readable.