Rails-jQuery Why do I have to click twice for jQuery effect to show? <p>I am using Rails 4.1.1 Why do I have to click twice for jQuery effect to show? I have been Googling but I cannot find any solutions, not sure if there's something wrong with my jQuery code or....</p> <p>When I click "Submit" for the first time, the effect didn't appear but the POST action is already called, when I click for the second time, the effect appeared and the POST action is called. </p> <p>Please check below my <code>create.js.erb</code>, <code>users_controller.rb</code>, <code>new.html.erb</code></p> <p><strong>create.js.erb</strong></p> <pre><code>function isValidEmailAddress(emailAddress) { var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&amp;'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&amp;'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i); return pattern.test(emailAddress); }; $("form").submit(function( event ) { var email = $("input#user_email").val(); if (email == "") { $("h1").text( "Email cannot be empty" ).show().fadeOut( 3000 ); return; } else if (!isValidEmailAddress(email)) { $("h1").text( "Email address is not valid" ).show().fadeOut( 3000 ); }else { $("h1").text( "Correct email address" ).show(); } event.preventDefault(); }); </code></pre> <p><strong>new.html.erb</strong></p> <pre><code>&lt;%= form_for @user, remote: true do |f| %&gt; &lt;p&gt; &lt;%= f.text_field :email %&gt; &lt;/p&gt; &lt;p&gt; &lt;%= f.submit :Submit %&gt; &lt;/p&gt; &lt;% end %&gt; &lt;h1&gt;&lt;/h1&gt; &lt;% @users.each do |user| %&gt; &lt;tr&gt; &lt;li&gt;&lt;%= user.email %&gt;&lt;/li&gt; &lt;/tr&gt; &lt;% end %&gt; </code></pre> <p><strong>users_controller.rb</strong></p> <pre><code>class UsersController &lt; ApplicationController def new @users = User.all @user = User.new end def create @user = User.new(user_params) respond_to do |format| format.html { redirect_to '/' } format.js end @user.save end private def user_params params.require(:user).permit(:email) end end </code></pre>