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

109
Views
Alpine js get original element in bind method

Assuming I want to get the original element within the Alpine.bind method, how would I do that? So I can retrieve properties like $el.targetName,$el.style, $el.onclick etc For example, if I have :

<script>
    document.addEventListener('alpine:init', () => {
        Alpine.bind('SomeButton', () => ({
            type: 'button',
 
            '@click'() {
                // How would  i get the target element with ll its DOM properties
                //So I can retrive properties like  $el.targetName,$el.style, $el.onclick etc
            },
 

        }))
    })
</script>

I've tried $el, this(which only returns a js proxy) etc

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

0

In a component definition you have to prefix every object with this. to access the active instance of the component. So the solution is just to use this.$el to access the button HTML element:

<button x-data x-bind="SomeButton">Click here</button>

<script>
document.addEventListener('alpine:init', () => {
  Alpine.bind('SomeButton', () => ({
      type: 'button',

      '@click'() {
          console.log(this.$el.textContent)
      },
  }))
})
</script>

This puts Click here in the console on click.

Note: don't forget to add x-data to the button if it does not have an Alpine.js parent element.

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!