Stange behaviour of dropdown in Liveview

Choose option 10, url patched to http://localhost:4000/test?per_page=10
Choose option 15, url patched to http://localhost:4000/test?per_page=15
Click on browser back button. I expect the browser to reload url http://localhost:4000/test?per_page=10 and see the dropdown at option 10. However the dropdown stays at option 15. :confused:

I also looked at the server logs to make sure that handle_params is indeed triggered and that options is updated

What’s happening here? Is this a limitation?

defmodule LiveViewStudioWeb.TestLive do
  use LiveViewStudioWeb, :live_view

  def mount(_params, _session, socket) do
    {:ok, assign(socket, options: %{})}

  def handle_params(params, _uri, socket) do
    per_page = (params["per_page"] || "5") |> String.to_integer()

    options = %{per_page: per_page}


    {:noreply, assign(socket, options: options)}

  def handle_event("submit", %{"per_page" => per_page}, socket) do
    options = %{socket.assigns.options | per_page: per_page}
    socket = update(socket, :options, fn _ -> options end)
    {:noreply, push_patch(socket, to: ~p"/test?#{options}")}

  def render(assigns) do
    <form phx-change="submit">
      <select name="per_page" id="">
        <%= Phoenix.HTML.Form.options_for_select([5,10,15], @options.per_page) %>

So when hitting the back button, did the server logs show handle_params being triggered and options updated or was handle_params never hit at all? And did the url update correctly in the browser when you hit the back button?

1 Like

handle_params was hit and the url was updated. However the drop-down list shown on browser is unchanged I.e it’s not aligned with the url

Looks like select with focus is not updated, and all options inside are not updated as well. No idea why. But there are some closed github issues related to this which suggest workarounds:

So one of workarounds which works for your example is to add dynamic id to select and force update:

<select name=“per_page” id={“per_page#{@options.per_page}”}>


I second this. Regular input text elements do update their values, but not the <select> element.