Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

115
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda