Skip to content

Instantly share code, notes, and snippets.

@yuxufm
Last active October 20, 2022 09:43
Show Gist options
  • Save yuxufm/434858ae421d9d0e4ee0df41a5d566ff to your computer and use it in GitHub Desktop.
Save yuxufm/434858ae421d9d0e4ee0df41a5d566ff to your computer and use it in GitHub Desktop.
add and remove DOM using Alpine.js
<script defer src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"></script>
<div x-data="{data: []}">
<div @click="data.push({ randomNumber: new Date().getTime() })">(Click Here To Add New Data)</div>
<template x-for="(item, index) in data" :key="index">
<div>
<span x-text="item.randomNumber"></span>
<span @click="data.splice(index, 1)">Remove</span>
</div>
</template>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment