Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$nextTick directly from a Javascript script

I'm wondering if there's a way of use the $nextTick AlpineJS magic property in a javascript script (the "$nextTick" is not recognize):

<div x-data="{ app() }">
    <button
        @click="updateContent"
        x-text="title"
    ></button>
</div>
...
<script src="{{ asset('js/my_script.js') }}"></script>

my_script.js:

function app() {
    return {
        title: 'Hello',
        updateContent: function() {
            title = 'Hello World!';
            // $nextTick(() => { console.log($el.innerText) });
            // $ is not recognize
        }
    }
}
like image 926
Daniel Campos Avatar asked Feb 04 '26 22:02

Daniel Campos


1 Answers

This works for me:

Alpine.nextTick()

Like this:

export function example() {
  return {
    open() {
      ...
      Alpine.nextTick(() => {
        document.querySelector('#menu a')?.focus();
      });
      ...
    },

  };
}
like image 57
Fred K Avatar answered Feb 06 '26 12:02

Fred K