0

I have a SvelteKit app with an @error.svelte page at the root of the /routes folder. Inside of this page, I have two buttons. One is in the header and the other one trigger's a page event when clicked on. For some reason, neither of these buttons are triggering the on:click handler when clicked on.

Here is the simplified code

<script>
    import Button, { Label } from '@smui/button';
    function handleClick() { // Neither the smui button or the native html button run this function
        console.log('click');
    }
</script>
<div class="page-container">
    <Button
        variant="unelevated"
        on:click={handleClick}> <!--Nothing happens when clicked!-->
        <Label>Go Back To Homepage</Label>
    </Button> 
    <button on:click={handleClick}>Click me</button> <!--Nothing happens when clicked!-->
</div>

I have tried the following troubleshooting steps:

  1. Adding console logs to the function inside on:click to see if it's being called: The function isn't being called at all.
  2. My buttons are Svelte Material UI buttons. To ensure the error didn't come from this library, I created a test native html button with an on:click handler. This one didn't call the click function either.
  3. Checked the browser console. I don't see any errors.
  4. I looked at this post Thought that there might be an issue with both buttons being in @error.svelte located at the root of my /routes folder but I couldn't understand the answer that well. Assuming this answer is true, the problem would have been fixed in the native HTML button. But it wasn't.

Any clue?

2
  • (1) You haven't turned off client-side JS by mistake? (2) If the <button> is in a <form> it will submit the form when clicked, and a new page will be loaded, so you won't have any time to see the logged click message. Sure you haven't somehow encapsulated this component in a form? (3) In Svelte 5, we should no longer use on: to listen to events. Sure you're not using Svelte 5? Commented May 24, 2024 at 8:28
  • Thanks for the answer. What should be used on Svelte 5 to listen to events? Commented May 26, 2024 at 1:48

1 Answer 1

0

I've fixed my error by addressing an unrelated issue that was showing up in my console. Click events were being disabled due to this error

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.