I don't believe this is possible with Astro.
The frontmatter is always processed on the server, whether at build or render (SSG or SSR). Frontmatter variables are serialized with JSON.stringify()
.
The button's onclick is processed on the client, and can only utilize functions already available on the client.
Astro docs recommend using addEventListener
in a <script>
tag for onclick functionality: https://docs.astro.build/en/guides/client-side-scripts/#handle-onclick-and-other-events
---
import Layout from "@layouts/Layout.astro";
---
<Layout title="Hello, world" class="pt-10">
<h1>I'm wondering</h1>
<button id="tgt">Click me</button>
</Layout>
<script>
const tgt = document.querySelector("#tgt");
tgt.addEventListener("click", () => {
console.log("Hello, on client");
});
</script>
If you'd like a form on the client to run a function on the server: https://docs.astro.build/en/recipes/build-forms/