Well the body can’t have any dom elements according to the challenger
That means I had to create the dom elements as well and I was unable to figure out a way within the given character limit
f.addEventListener('keypress',e=>{if(e.keyCode==000) etc()}) f.addEventListener('keypress',e=>{...})
With the older but essentially equivalent f.onkeypress=e=>{...}
and save 18 characters. You can even use `onkeyup` and save 3 more characters.<body><script>let win=window;document.body.innerHTML=`<input id="a"/><b id="b"></b><ul id="c"></ul>`,win.b.onclick=a=>{let b=win.c.appendChild(document.createElement("p"));b.innerText=win.a.value,win.a.value="",b.onclick=()=>win.c.removeChild(b)};</script>
JS Fiddle: https://jsfiddle.net/2z0bus8d/
While I think there's always the standard caveat around code golf in JS (you're building on a vast set of APIs and an often resource hungry platform), reaching for a framework really doesn't feel in the spirit of this kind of puzzle.
I found it difficult manipulate DOM with just js without hitting the limit, well done sir, you deserve a cookie
Too late. Golfing notes:
• Your `win` variable is unnecessary: window is approximately the global object (look into globalThis for the subtleties of how window isn’t quite the global object), so you can drop your “win.” every time, e.g. win.c.removeChild(b) can be just c.removeChild(b).
• parent.appendChild(child) → parent.append(child) and parent.removeChild(child) → child.remove(), using newer, shorter DOM methods.
• Skip quotes on attribute values, and trailing slashes are 100% superfluous in HTML (they’re explicitly ignored, as an XHTML compatibility mechanism; I reckon they’re actually a slightly bad thing rather than harmless).
The first two are new to me, so thank you, I'll read up. The third succinctly explains a quirk I've never bothered to check the legitimacy of, so thank you again.
With your changes, that brings us way below the character limit, sitting at 208 characters with the <body> and <script> tags the challenge dictates.
I had to rejig things to use parent.append(child) as it doesn't return the element, but using the child.remove() method directly means there's no parameter and therefore one less character there, so it all balances out. I also had to rename the event variable as it caused a scoping conflict for the variable `a`.
With all that space left over you could certainly add in the clear all and strike-through behaviour properly. Maybe even some real state!
Updated fiddle: https://jsfiddle.net/2z0bus8d/1/
Combining yours & the strike thru example, managed to get both strike thru & clear added in 277 chars. If i could save another char could close the p which would be nice :p
https://jsfiddle.net/hysjgkcm/
Down to 270 https://jsfiddle.net/vr7246jk/1/
https://jsfiddle.net/eqym2ac0/
Working with shrew. 240 char with script/html or 217 with as JS.
--<body><script>…</script>
++<body onload="…">
(But again, would not fulfill the requirements of "sole script in body" :] )