So I have this simple todo list with an ajax call to update the field completed
in my database to either 1 or 0, when I click on a li item.
The code in my view is not good, obviously, and it ofcourse doesn't work. Only the first time, when I click on a li, it does get updated, but I need to refresh if I want it to work again, since the data-completed
doesn't get updated. I need jquery to do that, but I have no clue how to update a data-attribute to either 1 or 0 based on the original value.
Any pointers as to how to make this work properly will be greatly appreciated.
View:
<li data-id='{!! $task->id !!}' data-completed='@if($task->completed == '1') 0 @else 1 @endif'>
AJAX Call:
$('li').on('click', function(e){
$.ajax({
url: 'api/tasks/update/'+$(this).data('id')+'/'+$(this).data('completed'),
method: 'GET'
})
});
Since I am using laravel, this is my route:
Route::get('api/tasks/update/{id}/{completed}', function($id, $completed) {
$task = App\Task::find($id);
$task->completed = $completed;
$task->save();
});