Rails Version: 5.2.2
Chrome Version: 78.0.3904.87
While testing my website on Chrome today, I encountered an issue where the page automatically scrolls to the top whenever an AJAX request is made. This problem only occurs in Chrome and not in other browsers like Firefox. Despite trying to debug the root cause of this behavior, I have been unsuccessful in finding a solution.
Below is the structure of the form triggering the AJAX request:
<div id="container">
<%= form_for(obj, method: :post, url: url, remote: true) do |f| %>
<button id="obj-<%= obj.id %>-btn" type="button" class="btn btn-primary obj-btn" title="Add" data-toggle="tooltip" data-placement="left">
Add
</button>
<% end %>
</div>
Here is the JavaScript code responsible for submitting the form:
$("#container").on("click", "form > .obj-btn", function(e)
{
$(this).tooltip('hide');
var form = $(this).parent();
form.submit();
});
Upon sending the request to the server, the controller executes JavaScript contained in a create.js.erb
file.
I have explored various suggestions from different sources, such as adding e.preventDefault()
or return false;
in the submit handler, but these methods did not resolve the issue for me.
If anyone has any insights or solutions to this problem, your assistance would be greatly appreciated. Thank you.