nathanl

nathanl

LiveView: conditionally show an input when a `<select>` changes

I have what seems to be a simple problem to solve in a LiveView form, but for some reason I’m having trouble. It could be one of those old-fashioned brain fevers.

I have a boolean form input which, when toggled to true, should cause a secondary input to display so the user can enter details. It’s not this, but you can imagine that it is.

For the second input, I’m doing something like:

    <.input
      :if={@show_powers_input}
      field={@form[:powers]}
      type="text"
      label={gettext("Please describe your sweet powers")}
    />

So I just need to toggle @show_powers_input appropriately and I’m all set.

My first try was to use a phx-change, but that’s not optimal because it doesn’t make the second input appears as soon as the first one is modified; the user has to tab away from the first input to trigger the change event.

It seems like what I really want is the input event. According to CanIUse:

The input event is fired when the user changes the value of an <input> element, <select> element, or <textarea> element. By contrast, the “change” event usually only fires after the form control has lost focus.

Aside: if I’m understanding correctly, phoenix.js binds to both the ‘change’ and the ‘input’ event, but I don’t see the latter doing anything.

OK, so maybe I can use a hook to bind to input and trigger change, then let LiveView do its normal thing? I tried phx-hook="ChangeOnInput" with

    this.el.addEventListener("input", e => {
      console.log("hey an input", e);
      e.target.dispatchEvent(new Event('change', { 'bubbles': true }));
    })

… and while I do see my console log message, I still don’t get any event in my LiveView until the <select> loses focus.

What am I missing here?

Most Liked

nathanl

nathanl

As @Dabsy pointed out in Elixir Slack, a checkbox input would change immediately, so that makes more sense for this case. I suppose this question could still be relevant for someone where there are options other than true/false.

Where Next?

Popular in Questions Top

yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
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

We're in Beta

About us Mission Statement