Alpine.js x-bound: Bind Numeric Input
by rameel
HTML
<div x-data="{ a: 2, b: 7 }" x-format class="form">
<label>
<input type="number" &value="a" min="0" max="10" />
<input type="range" &value="a" min="0" max="10" />
</label>
<label>
<input type="number" &value="b" min="0" max="10" />
<input type="range" &value="b" min="0" max="10" />
</label>
<p>
{{ a }} + {{ b }} = {{ a + b }}
</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/@ramstack/alpinegear-bound@1/alpinegear-bound.min.js" defer></script>
<script src="https://cdn.jsdelivr.net/npm/@ramstack/alpinegear-format@1/alpinegear-format.min.js" defer></script>
<script src="https://cdn.jsdelivr.net/npm/alpinejs@3/dist/cdn.min.js" defer></script>
CSS
*,
*::before,
*::after {
box-sizing: border-box;
}
body {
font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
color: #0f172a;
}
h3 {
margin: 0;
}
.form {
display: flex;
flex-direction: column;
flex-basis: auto;
background: #fff;
padding: 1.5rem;
width: 100%;
min-width: 320px;
max-width: 480px;
border-radius: 4px;
border: 1px solid rgba(15, 23, 42, 0.15);
box-shadow: 0 6px 18px rgba(10, 15, 30, 0.06);
gap: 1rem;
}
label {
display: flex;
flex-direction: column;
gap: 0.5em;
font-size: 0.9rem;
font-weight: 600;
user-select: none;
}
input {
display: block;
width: 100%;
height: 2.5rem;
padding: 0.5rem 0.75rem;
font-size: 0.95rem;
border: 1px solid rgba(15, 23, 42, 0.15);
border-radius: 3px;
outline: none;
transition: border-color 0.1s, box-shadow 0.1s;
line-height: 1.3;
}
input:focus {
border-color: #2563eb;
box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}