Untitled

mail@pastecode.io avatar
unknown
javascript
8 days ago
1.3 kB
3
Indexable
Never
function saveTasks() {
    let tasks = [];
    $('#task-list li').each(function() {
        let task = {
            text: $(this).text(),
            completed: $(this).hasClass('completed')
        };
        tasks.push(task);
    });
    localStorage.setItem('tasks', JSON.stringify(tasks));
}

function loadTasks() {
    let tasks = JSON.parse(localStorage.getItem('tasks')) || [];
    tasks.forEach(function(task) {
        let taskItem = $('<li></li>');
        taskItem.text(task.text);
        if (task.completed) {
            taskItem.addClass('completed');
        }
        $('#task-list').append(taskItem);

        // Ponovno dodaj event listener-e za novo učitane zadatke
        taskItem.find('input[type="checkbox"]').on('change', function() {
            taskItem.toggleClass('completed');
            saveTasks();
        });

        taskItem.find('.delete-btn').on('click', function() {
            taskItem.remove();
            saveTasks();
        });
    });
}

// Pozivamo loadTasks pri učitavanju stranice
$(document).ready(function() {
    loadTasks();

    $('#add-task-btn').on('click', function() {
        // Dodavanje zadatka (kao u prethodnom primjeru)
        saveTasks(); // Ažuriramo Local Storage nakon dodavanja zadatka
    });
});
Leave a Comment