Untitled
unknown
javascript
13 days ago
1.1 kB
3
Indexable
Never
$('#add-task-btn').on('click', function() { let taskText = $('#new-task').val(); let priority = $('#priority-select').val(); if (taskText === "") return; let taskItem = $('<li></li>'); taskItem.html(`<span class="priority">[${priority}]</span> ${taskText} <button class="delete-btn">Obriši</button>`); // Sortiraj zadatke prema prioritetu let listItems = $('#task-list li'); let added = false; listItems.each(function() { let currentPriority = $(this).find('.priority').text().replace(/[[]]/g, ''); if (priorityOrder(priority) < priorityOrder(currentPriority)) { $(this).before(taskItem); added = true; return false; // Zaustavlja petlju } }); if (!added) { $('#task-list').append(taskItem); } $('#new-task').val(''); // Brisanje zadatka taskItem.find('.delete-btn').on('click', function() { taskItem.remove(); }); }); function priorityOrder(priority) { if (priority === 'High') return 1; if (priority === 'Medium') return 2; return 3; // Low }
Leave a Comment