javascript onClick event not fire #8836
-
It is very simple test. When I click <script>
import { onMount } from "svelte";
onMount(async () => {
function test(){
console.log('test')
}
});
</script>
<button onClick="stop" class="bg-green-400 p-2">stop</button> It shows error. Uncaught ReferenceError: test is not defined But if I try with this code, <script>
import { onMount } from "svelte";
onMount(async () => {
function test(){
console.log('test')
}
document.querySelector('button').addEventListener('click', test);
});
</script>
<button class="bg-green-400 p-2">stop</button> It works fine. I have no idea why it is discriminated. I use page options as like this in export const ssr = true;
export const prerender = true;
export const trailingSlash = 'always'; Is these option related with this phenomenon? Can someone explain why or point out where this discrimination is explained in documents ? Is Javascript events ( onClick .. ) not allowed in sveltekit ? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
You should write it like this: <script>
function test(){
console.log('test')
}
</script>
<button on:click={test} class="bg-green-400 p-2">stop</button>
I highly recommend going through the event handling section of the tutorial - it seems like you're missing some of the concepts there. |
Beta Was this translation helpful? Give feedback.
You should write it like this:
test
function inonMount
, it should just be at the top of the script tagonClick
, it useson:click
on:click={test}
I highly recommend going through the event handling section of the tutorial - it seems like you're missing some of the concepts there.