packages/docs/src/routes/tutorial/events/basic/index.mdx
A key feature of any framework is making it easy to listen to user events.
Qwik can listen on a variety of events by placing an on<Eventname>$ attribute on an element that subscribes to the corresponding browser event.
Your task is to add a click event handler on the <button> element. Start by adding the onClick$ property and bind it to a function that calls alert('Hello World!').
So far this may look like a normal framework behavior, however Qwik does things very differently from other frameworks.
Every time you see a $ you should be thinking, "lazy-loading happens here".
As you open the browser's developer tools, notice that no JavaScript is loaded until you click the button.
If JavaScript is required to subscribe to browser events, how does Qwik achieve this interaction without loading JavaScript?
Consider the following simplified HTML of the application below.
Tip: Click on the HTML tab to see actual HTML produced by the server.
<html>
<body>
<button on:click="chunk-abc.js#App_onClick">
Click Me
</button>
<script id="Qwikloader">....</script>
</body>
</html>
$ so the Qwik optimizer can transform your code for lazy-loading.$ is a signal to the optimizer to extract the code for lazy-loading.$ is a signal to you that lazy-loading "magic" happens at this point.$ should appear.$ and extracts the function wrapped by $ into a separate lazy-loadable chunk.<button> element as on:click attribute. Qwik then knows how to hook this event back up on the client.on:click attribute.on:click attribute contains:
The above behavior is what gives Qwik applications their instant-on property (resumability) without eagerly downloading and executing code and performing hydration which is expensive.
NOTE To avoid delays between user action and response, Qwik prefetches code in the background.