Advertisement
kstoyanov

01. Pet Me - vs2 - JS Exam - 27 June 2020

Oct 10th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function petMe() {
  2.   const dataField = document.querySelectorAll('input[type="text"]');
  3.  
  4.   const [nameField, ageField, kindField, currentOwnerField ] = dataField;
  5.  
  6.   const button = document.getElementsByTagName('button')[0];
  7.   button.addEventListener('click', findNewFriend);
  8.  
  9.   function findNewFriend(e) {
  10.     e.preventDefault();
  11.  
  12.     if (nameField.value === '' || ageField.value === '' || kindField.value === '' || currentOwnerField.value === '') {
  13.       return;
  14.     }
  15.     if (Number.isNaN(Number(ageField.value))) {
  16.       return;
  17.     }
  18.  
  19.     const getSection = document.getElementById('adoption');
  20.     const getList = getSection.getElementsByTagName('ul')[0];
  21.  
  22.  
  23.     const createListItem = document.createElement('li');
  24.     const createParagraph = document.createElement('p');
  25.  
  26.     const createFirstStrong = document.createElement('strong');
  27.     createFirstStrong.textContent = nameField.value;
  28.     createParagraph.appendChild(createFirstStrong);
  29.  
  30.     createParagraph.innerHTML += ' is a ';
  31.  
  32.     const createSecondStrong = document.createElement('strong');
  33.     createSecondStrong.textContent = ageField.value;
  34.     createParagraph.appendChild(createSecondStrong);
  35.  
  36.     createParagraph.innerHTML += ' year old ';
  37.  
  38.  
  39.     const createThirdStrong = document.createElement('strong');
  40.     createThirdStrong.textContent = kindField.value;
  41.     createParagraph.appendChild(createThirdStrong);
  42.  
  43.     createListItem.appendChild(createParagraph);
  44.  
  45.     getList.appendChild(createListItem);
  46.     createListItem.appendChild(createParagraph);
  47.  
  48.     const createSpan = document.createElement('span');
  49.     createSpan.textContent = `Owner: ${currentOwnerField.value}`;
  50.     createListItem.appendChild(createSpan);
  51.  
  52.  
  53.     const createButton = document.createElement('button');
  54.     createButton.textContent = 'Contact with owner';
  55.     createListItem.appendChild(createButton);
  56.  
  57.     createButton.addEventListener('click', changeButton);
  58.  
  59.  
  60.     nameField.value = '';
  61.     ageField.value = '';
  62.     kindField.value = '';
  63.     currentOwnerField.value = '';
  64.   }
  65.  
  66.   function changeButton(e) {
  67.     const box = e.target.parentNode;
  68.     box.getElementsByTagName('button')[0].remove();
  69.  
  70.     const createDiv = document.createElement('div');
  71.  
  72.     const createInput = document.createElement('input');
  73.     createInput.setAttribute('placeholder', 'Enter your names');
  74.  
  75.     const newButton = document.createElement('button');
  76.     newButton.textContent = 'Yes! I take it!';
  77.  
  78.     newButton.addEventListener('click', movePet);
  79.  
  80.     createDiv.appendChild(createInput);
  81.     createDiv.appendChild(newButton);
  82.  
  83.     box.appendChild(createDiv);
  84.   }
  85.  
  86.   function movePet(e) {
  87.     const getDiv = e.target.parentNode;
  88.  
  89.     const box = e.target.parentNode.parentNode;
  90.  
  91.     const inputField = getDiv.getElementsByTagName('input')[0];
  92.  
  93.     if (inputField.value === '') {
  94.       return;
  95.     }
  96.  
  97.     const getList = document.querySelector('#adopted ul');
  98.  
  99.     const owner = box.getElementsByTagName('span')[0];
  100.     owner.textContent = `New Owner: ${inputField.value}`;
  101.  
  102.     const newBtn = document.createElement('button');
  103.     newBtn.textContent = 'Checked';
  104.     box.appendChild(newBtn);
  105.  
  106.     newBtn.addEventListener('click', removePet);
  107.  
  108.     e.target.parentNode.remove();
  109.  
  110.     getList.appendChild(box);
  111.   }
  112.  
  113.   function removePet(e) {
  114.     e.target.parentNode.remove();
  115.   }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement