Darc Nawg
Darc Nawg

Reputation: 1655

Rails AJAX click method needs to be clicked twice

In rails 4, I'm trying to use jquery to hide a certain element on a page when the submit button is clicked. However, the element "span" is not hidden until I click the button a second time. This is my create.js.erb:

$(document).ready(function(){
  $("#button").click(function(){
    $("#actor").hide();
  });
});

This is my _form.html.erb:

<%= form_for(@actor, remote: true) do |f| %>
    <% if @actor.errors.any? %>
        <div id="errors">
            <p>Please correct the following <%= pluralize(@actor.errors.count, "error") %>:</p>
            <ul>
                <% @actor.errors.full_messages.each do |msg| %>
                    <li><%= msg %></li>
                <% end %>
            </ul>
        </div>
    <% end %>
    <div>
        <%= f.label :Name %>
        <%= f.text_field :fname %>
    </div>
    <div>
        <%= f.label :Surname %>
        <%= f.text_field :lname %>
    </div>    
    <div>
        <%= f.submit @actor.new_record? ? "Create actor" : "Update actor", :id => 'button' %>
    </div>
    <span id="actor">
        Test text...hide me please...
    </span>
<% end %>

I would like it to hide the element on the first click. Any advice?

Upvotes: 0

Views: 436

Answers (1)

Darc Nawg
Darc Nawg

Reputation: 1655

Removing the click method solved the problem.

Upvotes: 2

Related Questions