I'm having the oddest problem with a jquery/ajax html update form.
My app is a single page TODO App that uses a Rails controller, but all DOM changes take place through Jquery/Ajax. After the TODOs are rendered to the page through Javascript, there is an edit button for each TODO.
When the edit button is clicked, an API request is made that results in an update form being appended to the DOM.
However, when the update button from that form is clicked it isn't recognized by Jquery and for some unknown reason Rails re-renders the Index.html.erb page everytime. I've tried 10 different solutions for the selector and nothing works.
The method that selects appends the edit form to the DOM is here:
$(document).on("click", ".ugh3", function(e) {
e.preventDefault()
let id = e.target.id
let event = e
$.ajax({
type: "GET",
url: `/todos/${id}`,
success: function(response) {
let newTodo = new Todo(response)
let todoHtml = newTodo.formatEdit()
$('.todo-list').append(todoHtml)
}
})
})
The above call works perfectly. But when I try to get the form submit or on Click to work it never will recognize it and the Index.html.erb page is resubmitted. I've tried preventDefault and return false as shown below:
$("form.edit_todo").on("submit", function (e) {
debugger
e.preventDefault();
return false;
})
If anyone has a couple minutes to look at this I'd greatly appreciate it. This has been driving me insane for two days now. Thank very much!
The git repo is: https://github.com/jwolfe890/todoapp/blob/master/app/assets/javascripts/todo.js