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

412
Views
How do I get a button id or value from x-on:click in Alpine JS?

Looking to extract a value or id in Alpine JS onclick, I thought this.id or this.value would work but no luck.

Returns

Alpine Expression Error: Can't find variable: id

<button id="myBtn" value="100" x-on:click="alert(this.id);">Save</button>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

This might help you.

<button @click="alert($event.target.getAttribute('message'))" message="Hello World">Say Hi</button>

$event.target will give you access to the clicked element.

Ref: https://alpinejs.dev/directives/on#the-event-object

about 4 years ago · Juan Pablo Isaza Report

0

Pass the Event with $event. Then you can handle it like in vanilaJs.

function hello(e) {
  console.log(e.target.getAttribute('id'))
}
 <script defer src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"></script>    
<button id="myBtn" value="100" @click="hello($event);">Save</button>

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!