SHARE
TWEET

Untitled

a guest Apr 19th, 2019 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4. <head>
  5.     <title>Absent index</title>
  6.     <meta charset="UTF-8">
  7.     <meta name="viewport" content="width=device-width, initial-scale=1">
  8. </head>
  9.  
  10. <body>
  11.     <button type="submit" id="add">Add</button>
  12.     <section></section>
  13. </body>
  14. <script>
  15.     let section = document.querySelector('section');
  16.     let elementNumbers = new Set();
  17.     let absentElm = new Set();
  18.     const setAttributes = (elm, attrs) => { for (let key in attrs) elm.setAttribute(key, attrs[key]) };
  19.     const sorterSet = (oldSet) => {
  20.         let newSet = new Set([...oldSet].sort(function (a, b) { return a - b }));
  21.         return newSet;
  22.     }
  23.     const setRemover = (keys, arr) => {
  24.         for (prop of keys) arr.delete(prop);
  25.         return arr;
  26.     }
  27.     const clickSub = (e) => {
  28.         absentElm.add(+e.target.getAttribute('elmnum'));
  29.         document.querySelector('p#item_' + e.target.getAttribute('elmnum')).remove();
  30.     }
  31.     const clickAdd = () => {
  32.         let countElements = document.querySelectorAll('p').length;
  33.         countElements++;
  34.         let iteratorAbsElm = sorterSet(absentElm).values();
  35.         let absIdx = iteratorAbsElm.next().value || 0;
  36.         let newElementNumbers = sorterSet(setRemover(absentElm, elementNumbers));
  37.         let currentSelector = ('p#item_' + [...newElementNumbers][absIdx - 1]);
  38.         elementNumbers.add(absIdx || countElements);
  39.         absentElm.delete(absIdx);
  40.         let lineP = document.createElement('p');
  41.         let textP = document.createTextNode('Item ' + (absIdx || countElements) + ' ');
  42.         let buttonP = document.createElement('button');
  43.         let buttonText = document.createTextNode('Sub');
  44.         setAttributes(buttonP, {
  45.             'type': 'submit',
  46.             'class': 'sub',
  47.             'id': 'subItem_' + (absIdx || countElements),
  48.             'elmNum': (absIdx || countElements)
  49.         });
  50.         buttonP.appendChild(buttonText);
  51.         buttonP.addEventListener('click', (e) => clickSub(e))
  52.         lineP.setAttribute('id', 'item_' + (absIdx || countElements));
  53.         section.insertBefore(lineP, section.querySelector(currentSelector));
  54.         lineP.appendChild(textP);
  55.         lineP.appendChild(buttonP);
  56.     }
  57.     document.querySelector('button#add').addEventListener('click', clickAdd);
  58. </script>
  59.  
  60. </html>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top