Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. function solution() {
  2.  
  3. const btn = document.querySelector('button');
  4. const giftInput = document.querySelector('input');
  5.  
  6. const [addGifts,listGifts, sentGifts, discartGifts] = Array.from(document.querySelectorAll('h2'));
  7.  
  8.  
  9. btn.addEventListener('click', function(e){
  10. e.preventDefault();
  11.  
  12. const gift = giftInput.value;
  13.  
  14. const liList = document.createElement('li');
  15. const sendBtn = document.createElement('button');
  16. const discardBtn = document.createElement('button');
  17.  
  18. liList.classList.add('gift');
  19.  
  20. sendBtn.id = 'sendButton';
  21. sendBtn.textContent = 'Send';
  22.  
  23. discardBtn.id = 'discardButton';
  24. discardBtn.textContent = 'Discard';
  25.  
  26. liList.textContent = `${gift}`;
  27.  
  28. //click on the buttons
  29. sendBtn.addEventListener('click', function(e){
  30. const newLi = document.createElement('li');
  31. newLi.classList.add('gift');
  32. newLi.textContent = `${gift}`;
  33. sentGifts.appendChild(newLi);
  34.  
  35. this.parentNode.parentNode
  36. .removeChild(this.parentNode);
  37. });
  38.  
  39. discardBtn.addEventListener('click', function(e){
  40. const newLi = document.createElement('li');
  41. newLi.classList.add('gift');
  42. newLi.textContent = `${gift}`;
  43. discartGifts.appendChild(newLi);
  44.  
  45. this.parentNode.parentNode
  46. .removeChild(this.parentNode);
  47. });
  48.  
  49. if(listGifts.childElementCount === 0){
  50. listGifts.appendChild(liList);
  51. } else {
  52. for(let i = 0; i<listGifts.childElementCount; i++){
  53. let currentGift = listGifts.getElementsByTagName('li')[i];
  54.  
  55. if(liList.textContent.localeCompare(currentGift.textContent) === -1 ){
  56. listGifts.insertBefore(liList,currentGift);
  57. break;
  58. }
  59. }
  60.  
  61. }
  62.  
  63. liList.appendChild(sendBtn);
  64. liList.appendChild(discardBtn);
  65.  
  66. giftInput.value = '';
  67.  
  68. });
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement