Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.57 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.     <head>
  4.         <meta charset="UTF-8">
  5.         <title>Get CSS selectors</title>
  6.     </head>
  7.  
  8.     <body>
  9.         <h1>Create a table with the content of CSS selectors</h1>
  10.         <div class="instr">Write some CSS selectors(separated by ';'), press Enter and the Javascript code will create a table with the inner Html of those<br>
  11.         Try: p;.expclass;#expid</div>
  12.         <p>This is the content of a simple paragraph tag</p>
  13.         <div class="expclass"> This is the content of a tag with a class</div>
  14.         <div id="expid"> This is the content of a tag with id </div>
  15.        
  16.     </body>
  17.    
  18.     <script>
  19.         var input = document.createElement("INPUT");
  20.         input.setAttribute("type", "text");
  21.         input.setAttribute("value", "Hello World!");
  22.         input.setAttribute("id", "myText");
  23.         document.body.appendChild(input);
  24.         var text;
  25.         input.addEventListener("keyup", function(event) {
  26.           if (event.keyCode === 13) {
  27.             event.preventDefault();
  28.             text=document.getElementById("myText").value;
  29.             createTable(text);
  30.           }
  31.         });
  32.         function createTable(text) {
  33.             var selectors=text.split(";");
  34.             var body=document.body, tbl  = document.createElement('table');
  35.             tbl.style.border = '1px solid black';
  36.             selectors.forEach(function(el) {
  37.                 var appear=document.querySelectorAll(el);
  38.                 appear.forEach(function(txt) {
  39.                     var tr = tbl.insertRow();
  40.                     var td = tr.insertCell();
  41.                     td.appendChild(document.createTextNode(txt.innerHTML));
  42.                 });
  43.             });
  44.             body.appendChild(tbl);
  45.         }
  46.     </script>
  47. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement