Skip to content

Instantly share code, notes, and snippets.

@jhahspu
Last active October 25, 2022 06:41
Show Gist options
  • Save jhahspu/eaab39203529bc0ff29fc48177632d21 to your computer and use it in GitHub Desktop.
Save jhahspu/eaab39203529bc0ff29fc48177632d21 to your computer and use it in GitHub Desktop.
AlpineJS

Two Way Binding

<!-- Example 1 -->
<div x-data="{ input : '' }" class="flex justify-center mt-8">
    <input type="text" x-model="input" class="p-2 border-2 rounded shadow">
    <h3 class="mt-2"> Output:</h3>
    <p x-text="input" class="bg-red-100"></p>
</div>

<!-- Example 2 -->
<div x-data="{ myInput : 'Write Something' }">
    <input type="text" x-model="myInput" class="p-2 border rounded shadow border-1">
    <input type="text" x-model="myInput" class="p-2 border rounded shadow border-1">
</div>

<!-- Example 3 -->
<div x-data="{ myColor:'' }" class="flex justify-center mt-8">
    <select x-model="myColor" class="p-2">
        <option value="" disabled>Select A Color</option>
        <option>Green</option>
        <option>Red</option>
        <option>Yellow</option>
    </select>
    Select Color: <p x-text="myColor"></p>
</div>

<!-- Input directives -->
x-model.lazy="myInput"
x-model.number="myInput"
x-model.debounce.1000ms="myInput"
x-model.throttle.500ms="myInput"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment