How to get form element id's?

Hi there,

I’m going to start with an example:

<% form_for(@post) do |f| %>

<%= f.label :title %>
<%= f.text_field :title %>

<% end %>

Generates:

Title

Is there any way to get the input id as a string out of the form (f)
object? The example is quite simple, but if there are nested forms there
are id’s like thread_posts_attributes_0_title and I need that id to
modify the value with JavaScript. Hope there’s a solution.

Thanks!

You could just specify the id yourself, rather than trying to
read/replicate the auto-generated one, is that an option? That way you
know for sure what it’s going to be called, which will make it easier to
refer to in your JS.

If so then use the :id option and make sure you give it a unique id,
like “post_#{post.id}_name” or something.

Max W. wrote:

You could just specify the id yourself, rather than trying to
read/replicate the auto-generated one, is that an option? That way you
know for sure what it’s going to be called, which will make it easier to
refer to in your JS.

If so then use the :id option and make sure you give it a unique id,
like “post_#{post.id}_name” or something.

That is indeed an option. Thank you very much!