| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
A custom Datastar attribute plugin that runs an expression when the element is removed from the DOM.
<button
data-on:click="el.remove()"
data-on-remove="console.log('I removed myself', el)"
>
Click to remove
</button>The plugin expects you to provide an import map that specifies the location of the datastar module. For example:
<script type="importmap">
{
"imports": {
"datastar": "https://cdn.jsdelivr.net/gh/starfederation/datastar@1.0.0-RC.6/bundles/datastar.js"
}
}
</script>
<script type="module" src="https://cdn.jsdelivr.net/gh/regaez/data-on-remove@1.0.0/data-on-remove.min.js"></script>There are various reasons, some better than others, why you might want to be able to run an expression when an element is removed from the DOM. For example:
The data-on-remove attribute runs Datastar expressions when the element it is attached to is removed from the DOM. Within these expressions, you have access to the following special variables:
If the data-on-remove attribute is removed from the element prior to the element itself being removed, then the expression will not run.
<section>
<button
data-on:click="el.remove()"
data-on-remove="console.log({ el, parent })"
>
Click to remove
</button>
<section>When the button is clicked, this will log { el: button, parent: section } to the console.
Since the expression is executed after the element has already been removed from the DOM, you cannot access the parent via el.parentElement, so it is provided for you by the plugin.
MIT
| Back | FazBrowse Home | New Git URL |