Skip to content

Instantly share code, notes, and snippets.

@ffflabs
Created January 28, 2023 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ffflabs/0e9bb354d73eb4b93f7b4b129993950c to your computer and use it in GitHub Desktop.
Save ffflabs/0e9bb354d73eb4b93f7b4b129993950c to your computer and use it in GitHub Desktop.
alpinejs $watch issue
<script defer src="https://unpkg.com/alpinejs@3.1.0/dist/cdn.min.js"></script>
<form x-data="form()" x-on:submit.prevent="submit">
<div>
<label>
A
<input x-model="a" />
</label>
</div>
<div>
<label>
B
<select x-model='b'>
<option>foo</option>
<option>bar</option>
</select>
</label>
</div>
<button>Submit</button>
</form>
function form() {
function submit() {
console.log(this.a);
}
return {
init() {
this.$watch('b', () => this.submit());
},
a: '',
b: 'foo',
submit
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment