Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solution() {
- const btn = document.querySelector('button');
- const giftInput = document.querySelector('input');
- const [addGifts,listGifts, sentGifts, discartGifts] = Array.from(document.querySelectorAll('h2'));
- btn.addEventListener('click', function(e){
- e.preventDefault();
- const gift = giftInput.value;
- const liList = document.createElement('li');
- const sendBtn = document.createElement('button');
- const discardBtn = document.createElement('button');
- liList.classList.add('gift');
- sendBtn.id = 'sendButton';
- sendBtn.textContent = 'Send';
- discardBtn.id = 'discardButton';
- discardBtn.textContent = 'Discard';
- liList.textContent = `${gift}`;
- //click on the buttons
- sendBtn.addEventListener('click', function(e){
- const newLi = document.createElement('li');
- newLi.classList.add('gift');
- newLi.textContent = `${gift}`;
- sentGifts.appendChild(newLi);
- this.parentNode.parentNode
- .removeChild(this.parentNode);
- });
- discardBtn.addEventListener('click', function(e){
- const newLi = document.createElement('li');
- newLi.classList.add('gift');
- newLi.textContent = `${gift}`;
- discartGifts.appendChild(newLi);
- this.parentNode.parentNode
- .removeChild(this.parentNode);
- });
- if(listGifts.childElementCount === 0){
- listGifts.appendChild(liList);
- } else {
- for(let i = 0; i<listGifts.childElementCount; i++){
- let currentGift = listGifts.getElementsByTagName('li')[i];
- if(liList.textContent.localeCompare(currentGift.textContent) === -1 ){
- listGifts.insertBefore(liList,currentGift);
- break;
- }
- }
- }
- liList.appendChild(sendBtn);
- liList.appendChild(discardBtn);
- giftInput.value = '';
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement