Tags

FZ Frank Zappa Zappa

Press Enter or comma to add tags

function handleTagInput(event) { if (event.key === 'Enter' || event.key === ',') { event.preventDefault(); const input = event.target; const tags = input.value.split(',').map(tag => tag.trim()).filter(tag => tag.length > 0); if (tags.length > 0) { updateTags(tags); input.value = ''; } } } function removeTag(button) { const tag = button.dataset.tag; updateTags([], [tag]); } function updateTags(tagsToAdd = [], tagsToRemove = []) { const currentTags = Array.from(document.querySelectorAll('#tags-container span')) .map(span => span.textContent.trim()) .filter(tag => !tagsToRemove.includes(tag)); const newTags = [...new Set([...currentTags, ...tagsToAdd])]; fetch('/quotes/1/update_tags', { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]').content }, body: JSON.stringify({ quote: { tag_list: newTags.join(', ') } }) }) .then(response => response.json()) .then(data => { if (data.tags) { const tagsContainer = document.getElementById('tags-container'); tagsContainer.innerHTML = data.tags.map(tag => ` ${tag.name} ` ).join(''); } }) .catch(error => console.error('Error:', error)); }