why

why

Liveview: phx-click resets form input

Hello,

on a LiveView page, I have a form whose single text input gets cleared when I click on a separate, unrelated div that uses phx-click.

I would like the input not to be affected by the phx-click, if possible. Is there a way to accomplish this, or is this a limitation in LiveView?

I pasted my code below. Steps to reproduce:

  1. Type some text (one or two sentences) into the input (without submitting)
  2. Click on the “click me” text
  3. Input field is now cleared
  def render(assigns) do
    ~L"""

    Live Text: <%= @live_text%>

    <form phx-change="type_live" phx-submit="submit", placeholder="Type something...">
      <input type="text" name="q" value="<%= @query %>"/>
    </form>

    <div phx-click="test" phx-value-test="successfully clicked"><%= @test%></div>

    """
  end

  def mount(_params, _session, socket) do
    {:ok, assign(socket, query: nil, live_text: "", test: "click me")}
  end

  def handle_event("type_live", %{"q" => query}, socket)  do
    live_text = query
    {:noreply, assign(socket, live_text: live_text)}
  end

  def handle_event("submit", %{"q" => query}, socket) do
    {:noreply, assign(socket, query: query)}
  end

  def handle_event("test", %{"test" => test}, socket) do
    {:noreply, assign(socket, test: test)}
  end

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I think this is because as you were typing, the text field had focus, and phoenix won’t overwrite a form field that is actively in focus and being typed in. Otherwise there would be race conditions between the text being entered and the server’s knowledge / re-rendering of the field being actively typed in. When you clicked on the div, the input lost focus and could be overwritten.

Also Liked

why

why

Ah, that makes a ton of sense, thanks!

Btw, the reason why I’m using the @live_text assign at all (rather than just @query) is that I wanted to be sure that phx-change isn’t somehow locally (via js) updating the value of @query. The data seemed to come back so incredibly quickly from the remote server that I wanted to be double sure… LV is pretty incredible…

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement