Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve() {
- const inputElements = document.querySelectorAll('#container input');
- const addButton = document.querySelector('#container button');
- const petList = document.querySelector('#adoption ul');
- const adoptedList = document.querySelector('#adopted ul');
- const input = {
- name: inputElements[0],
- age: inputElements[1],
- kind: inputElements[2],
- owner: inputElements[3],
- }
- addButton.addEventListener('click', addPet)
- function addPet(e) {
- e.preventDefault();
- const name = input.name.value;
- const age = Number(input.age.value);
- const kind = input.kind.value;
- const owner = input.owner.value;
- if (!name || !age || age == '' || !kind || !owner) {
- return;
- }
- const liElement = document.createElement('li');
- const pElement = document.createElement('p');
- const strongNameElement = document.createElement('strong');
- strongNameElement.textContent = name;
- let innerNameText = document.createTextNode(' is a ');
- pElement.appendChild(strongNameElement);
- pElement.appendChild(innerNameText);
- const strongAgeElement = document.createElement('strong');
- let innerAgeText = document.createTextNode(' year old ');
- strongAgeElement.textContent = age;
- pElement.appendChild(strongAgeElement);
- pElement.appendChild(innerAgeText);
- const strongKindElement = document.createElement('strong');
- strongKindElement.textContent = kind;
- pElement.appendChild(strongKindElement);
- liElement.appendChild(pElement);
- const spanElement = document.createElement('span');
- spanElement.textContent = `Owner: ${owner}`;
- liElement.appendChild(spanElement);
- buttonContact = document.createElement('button');
- buttonContact.textContent = 'Contact with owner';
- liElement.appendChild(buttonContact);
- petList.appendChild(liElement);
- input.name.value = '';
- input.age.value = '';
- input.kind.value = '';
- input.owner.value = '';
- buttonContact.addEventListener('click', addContact);
- function addContact(e) {
- let currentLi = e.currentTarget.parentNode;
- currentLi.querySelector('button').remove();
- let divElement = document.createElement('div');
- let takeButton = document.createElement('button');
- takeButton.textContent = 'Yes! I take it!';
- let placeholderInputElement = document.createElement('input');
- placeholderInputElement.setAttribute = 'placeholder';
- placeholderInputElement.placeholder = 'Enter your names';
- divElement.appendChild(takeButton);
- divElement.appendChild(placeholderInputElement);
- currentLi.appendChild(divElement);
- takeButton.addEventListener('click', takePet);
- function takePet(e) {
- let currentPetLiElement = e.currentTarget.parentNode.parentNode;
- let ownerName = currentPetLiElement.querySelector('input');
- let checkOwner = ownerName.value;
- if (!checkOwner) {
- return;
- }
- let print = currentPetLiElement.querySelector('div');
- currentPetLiElement.querySelector('div').remove();
- let newOwner = currentPetLiElement.querySelector('span');
- newOwner.textContent = `New Owner: ${checkOwner}`;
- let checkButton = document.createElement('button');
- checkButton.textContent = 'Checked';
- currentPetLiElement.appendChild(checkButton);
- adoptedList.appendChild(currentPetLiElement);
- checkButton.addEventListener('click', (e) => {
- let print2 = e.currentTarget.parentNode.parentNode;
- console.log(print2);
- e.currentTarget.parentNode.parentNode.remove();
- });
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement