Advertisement
kstoyanov

01. Pet Me JS Advanced Exam - 27 June 2020

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