AlpineJS is a Javascript framework that is compatible with Livewire, a full-stack framework for Laravel. This framework allows web developers to quickly build interactive javascript components without the need for jQuery or hooks. Instead Alpine uses simple to javascript directives that are far more simple to implement.
AlpineJS Directives.
Here is a list of widely used directives used to work with AlpineJS.
- x-data
- x-bind
- x-on
- x-text
- x-html
- x-model
- x-show
- x-transition
- x-for
- x-if
- x-init
- x-effect
- x-ref
- x-cloak
- x-ignore
AlpineJS Counter
<div x-data="{count: 0}"> <button x-on:click="count++"> increment </button>
<span x-text="count"></span>
</div>
Almost all AlpineJS components will make use of the x-data directive. This is where you will declare your javascript object that Alpine will track. The x-on:click directive executes the code in the parameters when the component is clicked, in this case count is being incremented. The x-data directive simply takes an alpine object as a parameter and sets the text of the component to the value of the javascript object, in the case of the counter it will display the current count.
Drop Down Menu
<div x-data="{ open: false }"> <button @click="open = ! open">Toggle</button> <div x-show="open" @click.outside="open = false">Contents...</div> </div>
This is a simple implementation of a dropdown menu in AlpineJS. Basically, we are creating a toggle and simply setting the value of show dynamically to the value of the toggle. To improve the functionality and usability of this design, you would need to use a styled list instead of the text of the content and use the x-effect directive to add some simple animations.
For Loop
<ul x-data="{ colors: ['Red', 'Orange', 'Yellow'] }"><template x-for="color in colors">
</template>
<li x-text="color"></li>
</ul>
It is very simple to make a for loop in AlpineJS. You can simply use the x-for directive which takes a javascript object with contains an array however the x-for component must be a template. Then the code within the x-for div will be executed for each element in the array. In the case above we are iterating through the colour array and rendering each element text as a list.
Final thoughts
It is definitely worth taking a look at AlpineJS if you are a web developer who uses a Laravel and JavaScript stack. The simplicity that Alpine offers can not be found elsewhere when it comes to JavaScript components.

For more information check out the AlpineJS docs. https://alpinejs.dev/start-here