Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve() {
- let addForm = document.getElementById('add');
- let adoptionDiv = document.getElementById('adoption');
- let adoptedDiv = document.getElementById('adopted');
- let container = document.querySelectorAll("#container input");
- addForm.addEventListener('click',add);
- function add(e){
- e.preventDefault();
- if(e.target.localName === 'button'){
- let adoptionUl = adoptionDiv.querySelector('ul');
- let newItemLine = document.createElement('li');
- let p = document.createElement('p');
- p.innerHTML = `<strong>${_check(container[0].value)}</strong> is a <strong>${Number(container[1].value)}</strong> year old <strong>${_check(container[2].value)}</strong>`;
- let currentOwner = document.createElement('span');
- currentOwner.innerText = `Owner: ${container[3].value}`;
- let contactOwner = document.createElement('button');
- contactOwner.innerText = 'Contact with owner';
- newItemLine.appendChild(p);
- newItemLine.appendChild(currentOwner);
- newItemLine.appendChild(contactOwner);
- adoptionUl.appendChild(newItemLine);
- container[0].value = '';
- container[1].value = '';
- container[2].value = '';
- container[3].value = '';
- };
- };
- adoptionDiv.addEventListener('click',adopt);
- function adopt(e){
- if(e.target.textContent === 'Contact with owner'){
- let currentLi = e.target.parentNode;
- e.target.remove();
- let newDiv = document.createElement('div');
- let newInput = document.createElement('input');
- let btn = document.createElement('button');
- btn.textContent = 'Yes! I take it!';
- newInput.setAttribute('placeholder','Enter your names');
- newDiv.appendChild(newInput);
- newDiv.appendChild(btn);
- currentLi.appendChild(newDiv);
- btn.addEventListener('click', takeIt);
- function takeIt(){
- if(newInput.value !== ''){
- let containerUl = adoptedDiv.querySelector("ul");
- let newItemLine = document.createElement('li');
- let p = document.createElement('p');
- p.innerHTML = currentLi.querySelector("p").innerHTML;
- currentLi.remove();
- let currentNewOwner = document.createElement('span');
- currentNewOwner.innerText = `New Owner: ${newInput.value}`;
- let checked = document.createElement('button');
- checked.innerText = 'Checked';
- newItemLine.appendChild(p);
- newItemLine.appendChild(currentNewOwner);
- newItemLine.appendChild(checked);
- containerUl.appendChild(newItemLine);
- }
- }
- };
- };
- adoptedDiv.querySelector('ul').addEventListener('click', checked);
- function checked(e){
- if(e.target.textContent === 'Checked'){
- e.target.parentNode.remove();
- }
- }
- function _check(str){
- if(str === ''){
- throw new Error('Invalid input!')
- }
- return str;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment