theglasspenguin — 11/20/2022, 1:52:19 AM

ok i need help again with my code

so this is my current setup:

HTML:

<button type="button" id="okbutton" onclick="go()">OK</button>

JS:

function go() {
// stuff
}

every time i’ve clicked the button, nothing happens and i get "go()" is not defined in the console

♥ 1 ↩ 0 💬 11 comments

comments

lily:

definition needs be before button

<script>
function go() {...}
</script>
<button ... />

or you can just do

document.querySelector("#okbutton").addEventListener("click", (event) => {...});
11/20/2022, 11:46:58 AM
theglasspenguin:

i have the script imported from a tag in the <head>, so i would imagine it is before the button

11/20/2022, 8:02:18 PM
lily:

oh

11/20/2022, 10:13:21 PM
oren:

Learn svelte or sveltekit

11/20/2022, 2:35:09 AM
oren:

That's my #1 advice for web developers

11/20/2022, 2:35:40 AM
theglasspenguin:

never worked for me

11/20/2022, 2:42:21 AM
oren:

in sveltekit your app would look like this:

<script>
function go() {
  //stuff
}
</script>
<button type="button" id="okbutton" on:click={go}>OK</button>
11/20/2022, 2:37:18 PM
wynd:

are you deferring the scripts, like is the dom loading before the scripts or no

11/20/2022, 1:57:23 AM
wynd:

how are you importing the js / where is your script tag

11/20/2022, 1:57:53 AM
theglasspenguin:

using an ordinary script tag in the <head>. I’m deferring the main script but that’s causing issues with some dependency scripts imported through unpkg

11/20/2022, 2:11:15 AM
wynd:

dunno maybe put all your scripts right before the closing body tag

11/20/2022, 2:12:57 AM