Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

349
Views
Why doesn't Alpine.js work with all events from their examples?

I'm trying out Alpine.js, but some of the examples doesn't work for me. For example

<button x-on:click="alert('Hello World!')">Say Hi</button>

This doesn't alert anything.

<button x-on:click="console.log('test')">Log</button>

And this doesn't log anything into the console. However other things work:

<div x-data="{ count: 0 }">
    <button x-on:click="count--">Decrement</button>
    <code>count: </code><code x-text="count"></code>
    <button x-on:click="count++">Increment</button>
</div>

This is my whole page:

<!DOCTYPE html>
<html>
<body>

<h1>Works</h1>

<div x-data="{ count: 0 }">
    <button x-on:click="count--">Decrement</button>
    <code>count: </code><code x-text="count"></code>
    <button x-on:click="count++">Increment</button>
</div>

<div x-data="{ open: false }">
    <button @click="open = ! open">Expand</button>

    <div x-show="open">
        Content...
    </div>
</div>

<div x-data="{ title: 'Start Here' }">
    <h1 x-text="title"></h1>
</div>

<button x-data="{ label: 'Click Here' }" x-text="label"></button>


<h1>Doesn't work</h1>

<button x-on:click="console.log('test')">Log</button>

<button x-on:click="alert('Hello World!')">Say Hi</button>

<button @click="$event.target.remove()">Remove Me</button>

<div @foo="console.log('foo was dispatched')">
    <button @click="$dispatch('foo')">Dispatch</button>
</div>


<script defer src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"></script>
</body>
</html>

What am I doing wrong?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your examples that aren't working don't have an x-data attribute (either on themselves or a parent). Whether there's data or not, the x-data attribute is required to get AlpineJS functionality.

If you wrap the entire "Doesn't work" section in a <div x-data></div> they should work:

<div x-data>
    <button x-on:click="console.log('test')">Log</button>

    <button x-on:click="alert('Hello World!')">Say Hi</button>

    <button @click="$event.target.remove()">Remove Me</button>

    <div @foo="console.log('foo was dispatched')">
        <button @click="$dispatch('foo')">Dispatch</button>
    </div>
</div>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!