Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solution() {
- function makeEl(type, text, clazz, id) {
- let el = document.createElement(type);
- if (text !== null) {
- el.textContent = text;
- }
- if (clazz !== null) {
- el.setAttribute('class', clazz);
- }
- if (id !== null) {
- el.setAttribute('id', id);
- }
- return el;
- }
- function removeAndMoveToSection(ul, gift) {
- sections[1].children[1].removeChild(gift);
- gift.lastChild.remove();
- gift.lastChild.remove();
- ul.appendChild(gift);
- let items = [...ul.querySelectorAll('li')];
- ul.innerHTML = '';
- items.sort(((a, b) => a.textContent.localeCompare(b.textContent))).forEach(li => ul.appendChild(li));
- }
- let sections = document.querySelectorAll('section');
- let btn = sections[0].children[1].children[1];
- let input = sections[0].children[1].children[0];
- btn.addEventListener('click', function (e) {
- if (input.value === '') {
- console.log('Error');
- return;
- }
- let giftsList = sections[1].children[1];
- let gift = makeEl('li', input.value, 'gift', null);
- let sendBtn = makeEl('button', 'Send', null, 'sendButton');
- sendBtn.addEventListener('click', function () {
- removeAndMoveToSection(sections[2].children[1], gift);
- });
- gift.appendChild(sendBtn);
- let discardBtn = makeEl('button', 'Discard', null, 'discardButton');
- discardBtn.addEventListener('click', function () {
- removeAndMoveToSection(sections[3].children[1], gift);
- });
- gift.appendChild(discardBtn);
- giftsList.appendChild(gift);
- let items = [...giftsList.querySelectorAll('li')];
- giftsList.innerHTML = '';
- items.sort(((a, b) => a.textContent.localeCompare(b.textContent))).forEach(li => giftsList.appendChild(li));
- input.value = '';
- });
- }
Add Comment
Please, Sign In to add comment