How to Capture Text of Items When Clicked

From ElixirBlocks
Jump to: navigation, search

Example

defmodule AppWeb.PageLive do
  use AppWeb, :live_view


  def render(assigns) do
    ~H"""
    <div id="list-container">
      <ul>
        <%= for item <- @items do %>
          <li phx-click="capture_text" phx-value-text={item}><%= item %></li>
        <% end %>
      </ul>
    </div>
    """
  end



  def mount(_params, _session, socket) do
    items = ["Item 1", "Item 2", "Item 3"] # Example list items
    {:ok, assign(socket, items: items)}
  end

  def handle_event("capture_text", %{"text" => text}, socket) do
    IO.inspect(text) # Handle the captured text as needed
    {:noreply, socket}
  end


end