I'm creating a todo list, with each todo's HTML div containing a data-completed="true" attribute.
I am trying to toggle the completed attribute back and forth when the todo is clicked using Jquery
It switches to false, but when I click again it doesn't switch back. The flipCompleted variable seems to get stuck on false despite the fact the click event is passing in the new value.
$("body").on("click", ".todo", function() {
let id = $(this).data("id")
let completed = $(this).attr("data-completed")
// dbMethods.updateTodo(id, completed)
domMethods.updateTodo.call(this, completed)
event.stopPropagation()
})
updateTodo: function(completed) {
let flipCompleted = !completed
$(this).children('.todo-title').toggleClass("completed");
$(this).data("completed", flipCompleted);
}
HTML:
<div class="todo" data-id="5de7d31d753b914d6f714cab" data-completed="true">
<div class="todo-title"><h4>Go to shops</h4></div>
<button class="delete"><i class="fas fa-trash-alt" aria-hidden="true"></i></button>
</div>
I've tried changing it with .attr() and .data() both of which only work once.
I'm not sure what I am doing wrong so any help would be greatly appreciated.
let completed = $(this).data("completed")instead oflet completed = $(this).attr("data-completed").data()to set values won't affect the attribute.