3 ms·
And this is why code golf is fun regardless of language! The first two are new to me, so thank you, I'll read up. The third succinctly explains a quirk I've ne
by shrew 6y ago
And this is why code golf is fun regardless of language!
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/ https://jsfiddle.net/2z0bus8d/1/
- thybag 6y ago<script>x=document;v='innerHTML';o='onclick';x.body[v]="<input id=a><b id=b>X</b><p id=d>clear<ul id=c>",b[o]=e=>{let b=x.createElement("li");c.append(b);b[v]=a.value,a.value="",b[o]=()=>b.classList.toggle('y');d[o]=()=>c[v]=''};</script><style>.y{text-decoration:line-through} 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/ https://jsfiddle.net/hysjgkcm/ Down to 270 https://jsfiddle.net/vr7246jk/1/ https://jsfiddle.net/vr7246jk/1/
- thybag 6y ago<body><script>x=document;x.write`<input id=a><b id=b></b><p id=d>🆑<ul id=c>`,b[o='onclick']=b=>{b=x.createElement`li`;c.append(b);b.v=b[v='innerHTML']=a.value,a.value="",b[o]=e=>b[v]=(b[v]==b.v?'<strike>':'')+b.v;d[o]=e=>c[v]=''}</script> https://jsfiddle.net/eqym2ac0/ https://jsfiddle.net/eqym2ac0/ Working with shrew. 240 char with script/html or 217 with as JS.
- myfonj 6y agoNo JS-in-HTML golfing is complete until you cram your script into onload attribute: --<body><script>…</script> ++<body onload="…"> (But again, would not fulfill the requirements of "sole script in body" :] )
- chrismorgan 6y agoAh yes, I’d completely forgotten that appendChild returns the child element and append doesn’t, because it’s so long since I actually had cause to use the appendChild return value!