Hidden inputs - how to pass value using javascript?

I created a hidden input in my existing form and trying to pass value using javascript but not able to find out the id of that hidden input

Here is my code

<%= form_for @changeset, @action, fn f -> %>
  <%= if @changeset.action do %>
    <div class="alert alert-danger">
      <p>Oops, something went wrong! Please check the errors below.</p>
    </div>
  <% end %>

  <%= label f, :title %>
  <%= text_input f, :title %>
  <%= error_tag f, :title %>

  <%= label f, :description %>
  <%= textarea f, :description %>
  <%= error_tag f, :description %>

  <%= label f, :address %>
  <%= text_input f, :address %>
  <%= error_tag f, :address %>
    <%= hidden_input f, :image_public_id %>
    <%= error_tag f, :image_public_id %>

    <%= hidden_input f, :image_thumb_url %>
    <%= error_tag f, :image_thumb_url %>

    <%= hidden_input f, :image_url %>
    <%= error_tag f, :image_url %>

    <%= hidden_input f, :image_secure_url %>
    <%= error_tag f, :image_secure_url %>



  <div>
    <%= submit "Save" %>
  </div>
<% end %>

I’m not sure how can I set ids for last four fields

Are you referring to dom ids? If so, just pass them in as a keyword list after :name.

Like this:

<%= hidden_input f, :image_public_id, id: "id_1" %>
...
<%= hidden_input f, :image_thumb_url, id: "id_2" %>

# if passing class values to a regular text_input etc
<%= text_input f, :title, id: "title_id", class: "class1 class2" %>

I don’t think you’re going to want to add error_tags for the hidden inputs though.

More info here: Phoenix.HTML

Thank you very much brother

1 Like