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

193
Visualizações
How to access Alpine variables from normal JS?

I want to hide an element with Alpine after X number of seconds. So I need to run a setTimeout() that changes an x-data variable that the element's x-show is bound to.

I'm currently using the script tag with normal JS in, but I can't seem to access the x-data variables from it.

As an example:

<div x-data="showSomething = true">
    <script>
        setTimeout(() => {
            this.showSomething = false;
        }, 1500);
    </script>
</div>

I get this error whether I use "this." or not:

Uncaught ReferenceError: showSomething is not defined

How can I access the x-data variables from normal JS, or is there a way to go about this in Alpine?

Edit: I solved this by using x-on to listen for the event (and referring to the variables without .this within that function)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Use x-init.

<div
    x-data="{ showSomething: true }"
    x-init="setTimeout(() => { this.showSomething = false; }, 1500)"
>
</div>
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