A Pen by salmi iliass on CodePen.
Created
November 11, 2024 15:01
-
-
Save neisdev/13e01c914857b4a26268af3f2ac4600d to your computer and use it in GitHub Desktop.
Simple form input repeater with Alpine js 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="p-6 m-6 lg:p-12 lg:m-12 bg-gray-50 rounded-lg"> | |
<div class="flex flex-col lg:flex-row" x-data="{ fields: [''] }"> | |
<div class="lg:w-1/2 lg:mr-6 lg:mb-0 mb-6"> | |
<template x-for="(field, index) in fields" :key="index"> | |
<div class="flex mb-2"> | |
<input type="text" name="data[]" x-model="fields[index]" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" placeholder="write text here"> | |
<button x-show="fields.length > 1" class="ml-2 bg-red-500 hover:bg-red-700 text-white font-bold px-4 rounded focus:outline-none focus:shadow-outline" @click="fields.splice(index, 1)">-</button> | |
</div> | |
</template> | |
<button class="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline" @click="fields.push('')">+ Add field</button> | |
</div> | |
<div class="lg:w-1/2 bg-gray-800 px-6 py-4 rounded shadow-lg"> | |
<div class="text-md text-gray-500">DATA</div> | |
<pre x-text="JSON.stringify(fields, '', 2)" class='text-green-700'></pre> | |
</div> | |
</div> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdnjs.cloudflare.com/ajax/libs/alpinejs/3.2.1/cdn.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<link href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.2.4/tailwind.min.css" rel="stylesheet" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment