From f4fc630d1d7add9a42260194bf5bb72d488c2fcb Mon Sep 17 00:00:00 2001 From: Lapo Luchini Date: Mon, 29 Apr 2024 09:57:00 +0000 Subject: [PATCH] Only associate buttons' `onClick` when they exist. (to allow removing them) --- index.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 6d6cb49..2409180 100644 --- a/index.js +++ b/index.js @@ -125,18 +125,19 @@ export function decodeBinaryString(str) { } } // set up buttons -id('butDecode').onclick = function () { +const butClickHandlers = { + butDecode: () => { decodeText(area.value); -}; -id('butClear').onclick = function () { + }, + butClear: () => { area.value = ''; file.value = ''; tree.innerHTML = ''; dump.innerHTML = ''; selectDefs.innerHTML = ''; hash = window.location.hash = ''; -}; -id('butExample').onclick = function () { + }, + butExample: () => { console.log('Loading example:', examples.value); let request = new XMLHttpRequest(); request.open('GET', 'examples/' + examples.value, true); @@ -150,7 +151,13 @@ id('butExample').onclick = function () { } }; request.send(); + }, }; +for (const [name, onClick] of Object.entries(butClickHandlers)) { + let elem = id(name); + if (elem) + elem.onclick = onClick; +} // set dark theme depending on OS settings function setTheme() { let storedTheme = localStorage.getItem('theme');