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

185
Views
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 answers
Answer question

0

Use x-init.

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