Ivankooo1

pets

Oct 15th, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. function solve() {
  2. let addForm = document.getElementById('add');
  3. let adoptionDiv = document.getElementById('adoption');
  4. let adoptedDiv = document.getElementById('adopted');
  5. let container = document.querySelectorAll("#container input");
  6.  
  7. addForm.addEventListener('click',add);
  8. function add(e){
  9. e.preventDefault();
  10. if(e.target.localName === 'button'){
  11. let adoptionUl = adoptionDiv.querySelector('ul');
  12.  
  13. let newItemLine = document.createElement('li');
  14. let p = document.createElement('p');
  15. p.innerHTML = `<strong>${_check(container[0].value)}</strong> is a <strong>${Number(container[1].value)}</strong> year old <strong>${_check(container[2].value)}</strong>`;
  16.  
  17. let currentOwner = document.createElement('span');
  18. currentOwner.innerText = `Owner: ${container[3].value}`;
  19.  
  20. let contactOwner = document.createElement('button');
  21. contactOwner.innerText = 'Contact with owner';
  22.  
  23.  
  24. newItemLine.appendChild(p);
  25. newItemLine.appendChild(currentOwner);
  26. newItemLine.appendChild(contactOwner);
  27. adoptionUl.appendChild(newItemLine);
  28.  
  29. container[0].value = '';
  30. container[1].value = '';
  31. container[2].value = '';
  32. container[3].value = '';
  33.  
  34. };
  35.  
  36. };
  37.  
  38. adoptionDiv.addEventListener('click',adopt);
  39. function adopt(e){
  40. if(e.target.textContent === 'Contact with owner'){
  41.  
  42. let currentLi = e.target.parentNode;
  43. e.target.remove();
  44. let newDiv = document.createElement('div');
  45. let newInput = document.createElement('input');
  46. let btn = document.createElement('button');
  47. btn.textContent = 'Yes! I take it!';
  48. newInput.setAttribute('placeholder','Enter your names');
  49.  
  50. newDiv.appendChild(newInput);
  51. newDiv.appendChild(btn);
  52. currentLi.appendChild(newDiv);
  53.  
  54. btn.addEventListener('click', takeIt);
  55.  
  56. function takeIt(){
  57. if(newInput.value !== ''){
  58. let containerUl = adoptedDiv.querySelector("ul");
  59. let newItemLine = document.createElement('li');
  60. let p = document.createElement('p');
  61. p.innerHTML = currentLi.querySelector("p").innerHTML;
  62. currentLi.remove();
  63. let currentNewOwner = document.createElement('span');
  64. currentNewOwner.innerText = `New Owner: ${newInput.value}`;
  65.  
  66. let checked = document.createElement('button');
  67. checked.innerText = 'Checked';
  68.  
  69. newItemLine.appendChild(p);
  70. newItemLine.appendChild(currentNewOwner);
  71. newItemLine.appendChild(checked);
  72. containerUl.appendChild(newItemLine);
  73.  
  74. }
  75. }
  76. };
  77. };
  78.  
  79. adoptedDiv.querySelector('ul').addEventListener('click', checked);
  80. function checked(e){
  81. if(e.target.textContent === 'Checked'){
  82. e.target.parentNode.remove();
  83. }
  84. }
  85. function _check(str){
  86. if(str === ''){
  87. throw new Error('Invalid input!')
  88. }
  89. return str;
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment