PHP Classes

File: js/tags.js

Recommend this page to a friend!
  Classes of Ann   Purp Project   js/tags.js   Download  
File: js/tags.js
Role: Auxiliary data
Content type: text/plain
Description: Auxiliary data
Class: Purp Project
Application to manage photo albums
Author: By
Last change: Update of js/tags.js
Date: 1 year ago
Size: 1,792 bytes
 

Contents

Class file image Download
/* <div class="tag"> <span>Javascript</span> <span class="material-icons">close</span> </div> */ const tagContainer = document.querySelector('.tag-container'); const input = document.querySelector('.tag-container input'); var tags = []; function createTag(label) { const div = document.createElement('div'); div.setAttribute('class', 'tag'); const span = document.createElement('span'); span.innerHTML = label; const closeBtn = document.createElement('i'); closeBtn.setAttribute('class', 'material-icons'); closeBtn.setAttribute('data-item', label); closeBtn.innerHTML = 'close'; div.appendChild(span); div.appendChild(closeBtn); return div; } function reset(){ document.querySelectorAll('.tag').forEach(function(tag){ tag.parentElement.removeChild(tag); }) } function testUnique(A) { let n = A.length; for (let i = 0; i < n-1; i++) { for (let j = i+1; j < n; j++) { if (A[ i ] === A[j]) return false; } } return true; } function addTags(){ reset(); tags.slice().reverse().forEach(function(tag){ if(tag!=''){ const input = createTag(tag); tagContainer.prepend(input); } }) } input.addEventListener('keyup', function(e){ if(e.key == 'Enter'){ tags.push(input.value); addTags(); input.value = ''; } }) document.addEventListener('click', function(e){ if(e.target.tagName == 'I'){ const value = e.target.getAttribute('data-item'); const index = tags.indexOf(value); tags = [...tags.slice(0, index), ...tags.slice(index + 1)]; addTags(); } })