Advertisement
NyteOwlDave

Automatic Buttons from Table

Mar 7th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <meta charset="utf-8"/>
  2. <body></body>
  3. <script>
  4.  
  5. // Create a new unattached HTML element
  6. // and optionally set its innerText property
  7. function el(type,text) {
  8.   if (text) {
  9.     const e = el(type);
  10.     e.innerText = String(text);
  11.     return e;
  12.   }
  13.   return document.createElement(type);
  14. }
  15.  
  16. // Create a new unattached button element
  17. function button(title) {
  18.   return el('button', title);
  19. }
  20.  
  21. // Main routine
  22. ;(function() {
  23.  
  24.   // Table containing multiple rows and 2 columns per row
  25.   const table = [
  26.     ['b', 'ill'],
  27.     ['j', 'ill'],
  28.     ['f', 'ill'],
  29.     ['st', 'ill'],
  30.     ['c', 'at'],
  31.     ['b', 'at'],
  32.     ['r', 'at'],
  33.     ['s', 'at'],
  34.   ];
  35.  
  36.   // Dictionary of unique items
  37.   const dictionary = new Set();
  38.  
  39.   // Iterate the rows of the table
  40.   table.forEach(row=>{
  41.     // Add each item to the dictionary
  42.     // (this eliminates repeats)
  43.     dictionary.add(row[1].toUpperCase());
  44.   });
  45.  
  46.   // For each unique dictionary item
  47.   dictionary.forEach(item=>{
  48.     // Create a button whose text contains the item
  49.     const btn = button(item);
  50.     // Append the button to the HTML document's body element
  51.     document.body.appendChild(btn);
  52.   });
  53.  
  54. })();
  55.  
  56. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement