Repository navigation
Focus lost when using AlpineJS $id after the Livewire request is sent #3297
Replies: 10 comments 2 replies
|
I came here to report this. After spending about 4 hours trying to troubleshoot why my Stripe element was rerendering any time livewire sent a request, I was about to pull my hair out but then broke down each and every attribute in both Alpine and Livewire. Once I removed the "magical" I knew I should have looked at the issues... |
|
@johanvanhelden I'm having the same issue unfortunately. Did you manage to find a solution? |
Unfortunately not. |
It seems like adding |
Hmm, that does not sound like a good idea. If I understand things correctly, the key needs te be unique across the page due to how the DOM diffing works. So if you have multiple inputs, there will be duplicate empty keys. |
Yes that's what I thought too. It seems to work however, also having multiple fields with the empty wire:key attribute in the same livewire component and conditionally showing one of these inputs works as expected. Maybe @calebporzio can give some more information on this? |
|
I "solved" this by not using Pasting the code from the docs: <div x-data="{ id: $id('text-input') }">
<label :for="id"> <!-- "text-input-1" -->
<input type="text" :id="id"> <!-- "text-input-1" -->
</div>
<div x-data="{ id: $id('text-input') }">
<label :for="id"> <!-- "text-input-2" -->
<input type="text" :id="id"> <!-- "text-input-2" -->
</div> |
|
Thanks @TobiasFroberg . Just did the same thing in my project, works like a charm!! |
|
This issue has been converted to a discussion as we are now using discussions to track bugs and ideas. |
|
@TobiasFroberg Does this still work for you in Livewire v3? For me it only helps with Alpine not generating new ids for the labels (and thus disconnecting labels from inputs). It doesn't help me with the losing focus problem. |
Uh oh!
There was an error while loading. Please reload this page.
Discussed in #2826
Also discussed in the Livewire repo: livewire/livewire#4790
Originally posted by johanvanhelden April 11, 2022
Hi all! Not sure if this is a bug, or me not understanding something.
I was watching Caleb's Laracon talk and he was showing of how to use Alpine to generate ids for elements.
So I just finished implementing it, but after I type some data into an input element, and the Livewire request is sent to the server, the input looses its focus.
Demo:

The HTML for the input element looks like this:
If I revert the
$id()changes, everything works like it should, without anything loosing focus.Unfortunately the ID stays the same after the async request. Before the reload it is
input-2and after losing the focus it remainsinput-2. So that one does not change. I can actually not see any changes in the source. No part of the input group's HTML seems to change.Also posted in the Livewire discussions section, but it's a bit dead in there. So maybe it's more Alpine's fault?
livewire/livewire#4790
All reactions