Search code examples
javascriptruby-on-railsruby-on-rails-3ruby-on-rails-3.1ruby-on-rails-3.2

How to assign HTML id to elements and how apply JavaScript to them?


I developed a project but now I wants to apply JavaScript to project. Take an example of Login Form. In that I have to assign HTML id to text-fields but HOW? and how I can access the text-fields in js file?

    <% @page_title = 'Staff Area Login' -%>

    <%= form_for(:stuff, :url => {:action => 'send_login'}) do -%>

    <p>Username:  <%= text_field_tag('username', params[:username]) %></p>

    <p>Password: <%= password_field_tag('password') %></p>

    <%= submit_tag("Log in") %>

    <% end -%>

Solution

  • You can use the :html hash.

    <%= text_field_tag('username', params[:username], :html => {:id => "username_field"}) %>