Prepopulate x-model with value of input #1985
Replies: 8 comments 24 replies
-
3 kinda exists already |
Beta Was this translation helpful? Give feedback.
-
I like the idea of the Makes sense to treat the initial value attribute as an input event, though |
Beta Was this translation helpful? Give feedback.
-
Completely agree. Personally, I like option 2 the best, i.e. values of |
Beta Was this translation helpful? Give feedback.
-
Any news here? |
Beta Was this translation helpful? Give feedback.
-
Would also love this functionality as my use case is sprinkling interactivity into legacy Perl-driven templates. Currently I'm using <div x-data="{ optoutType: '' }">
<select x-model="optoutType" id="id__optout_type" name="sms_optouts">
<option value="" <TMPL_IF EXPR="!sms_optouts">selected</TMPL_IF>>Email Addresses</option>
<option value="1" <TMPL_IF EXPR="sms_optouts">selected</TMPL_IF>>Phone Numbers</option>
</select>
</div> Suppose "Phone Numbers" is selected on page load, |
Beta Was this translation helpful? Give feedback.
-
I'm trying out this as a workaround: <form x-data="{
username: $refs.username.value,
}">
<input x-ref="username" x-model="username" value="something">
</form> I haven't tested it much yet, but so far seems to be working well. |
Beta Was this translation helpful? Give feedback.
-
If I understand correctly, the idea is to force the value bound by x-model to initially be set to the input's value only if the bound x-model value is either null or an empty string? So this?
But then using a modifier like for instance something like so?
|
Beta Was this translation helpful? Give feedback.
-
Of all the APIs I like yours the best @Restartz Here is my low-effort scratch pad of ideas: I'm still not totally convinced we should add anything to core to address this. It would be nice to gauge demand on a broader scale. Like I'm sure more people want this than are represented in this discussion, but how many more? I'd hate to add a bespoke API that we have to document and maintain forever and very few people ever use it or no about it. Another reason I'm super hesitant with this stuff is every little feature adds to the amount of JS that has to be shipped to browsers (as opposed to adding APIs to a backend framework). That said, it seems like it would have a fairly simple, few line, implementation. So maybe just put the PR up and we can decide a little further down the road. Thanks for the input everyone. Oh also, I DO see the use case for this in a rails-esque environment where you want to use Alpine to augment form inputs. But I just wonder if a developer encountering this hurdle will make their way to this API and be satisfied. Of course it's ideal to make this the default behavior. But alas, as mentioned, my gut says that 's a breaking change (and not a bug, but I'd love to be convinced otherwise) |
Beta Was this translation helpful? Give feedback.
-
Hi,
alpinejs is great! it is exactly what the frantic javascript world needs, just enough to get away with and be productive as hell. Thanks for that!
I want to use alpinejs in a Rails project. There the input fields are rendered by the server and may contain a value. Now, when I want to implement a simple show/hide logic for a dependent field, I would like to use
x-model
(I explored other options, but this seemed the most elegant one). The only trouble is that since I defined thex-data
attribute on the parent, the server-given value is ignored. I would have to write dedicated logic to add the server value to the parent'sx-data
. This seems exhausting. Here an example:Now, I have three ideas, but would be happy if there are more:
x-model
which allows to force the the copying from the value initially (e.g.x-model.attain
)x-data
declaration of the variable wasundefined
,null
or another dedicated valuex-set="myinput"
)What do you think?
Beta Was this translation helpful? Give feedback.
All reactions