Easy way to toggle a boolean value in elixir phoenix

Hi there,

I’m new to Elixir and am trying to toggle a boolean value like JS :grin: but i think that is not possible. is there an easy way to do that?

 def update(assigns, socket) do
    socket
    |> assign(:show_password, false)
    |> reply(:ok)
  end
 
def handle_event("show_password", _params, socket) do
    socket
    |> assign(:show_password, !:show_password)
    |> reply(:noreply)
  end

thank you all

Hello and welcome,

The syntax would be correct, but it’s just an atom, not the previous value, which is… socket.assigns.show_password

3 Likes
def handle_event("show_password", _params, socket) do
    socket
    |> update(:show_password, &(!&1))
    |> reply(:noreply)
  end
5 Likes