ivana_andreevska

AV5 Zadaca3

Nov 25th, 2021
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //HTML
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5.     <meta charset='utf-8'>
  6.     <meta http-equiv='X-UA-Compatible' content='IE=edge'>
  7.     <title>Page Title</title>
  8.     <meta name='viewport' content='width=device-width, initial-scale=1'>
  9.     <link rel='stylesheet' type='text/css' media='screen' href='style.css'>
  10.     <script src='script.js'></script>
  11. </head>
  12.  
  13.  
  14. <body>
  15.     <h2>Projects details</h2>
  16.     <div><label>Author</label><input id="author" type="text"></div>
  17.     <div><label>Project</label><input id="project" type="text"></div>
  18.     <div><label>Cost</label><input id="cost" type="text"></div>
  19.     <div><input type="button" name="add" value="Add" onclick="addRow();"></div>
  20.     <h2>Project list - Total cost: <span id="total">0</span></h2>
  21.     <table id="projects">
  22.     <thead><th>Author</th><th>Project</th><th
  23.     colspan="2">Cost</th></thead>
  24.     <tbody>
  25.     </tbody>
  26.     </table>
  27. </body>
  28. </html>
  29.  
  30. //CSS
  31. label{
  32.     display: inline-block;
  33.     width: 100px;
  34. }
  35. div{
  36.     margin-bottom: 5px;
  37. }
  38. input[type=button]
  39. {
  40.     width:265px;
  41. }
  42. table , td , th{
  43.     border: 1px solid black;
  44.     border-collapse: collapse;
  45.     padding:10px;
  46. }
  47.  
  48.  
  49. //JS
  50. function addRow() {
  51.     var tableRef = document.getElementById('projects').getElementsByTagName('tbody')[0];
  52.     var project = document.getElementById('project');
  53.     var author = document.getElementById('author');
  54.     var cost = document.getElementById('cost');
  55.  
  56.     if(project.value == "" || author.value == "" || cost.value == "") {
  57.     alert("All values are not entered.");
  58.     return;
  59.     }
  60.     var newRow = tableRef.insertRow(tableRef.rows.length);
  61.     var newCell0 = newRow.insertCell(0);
  62.     var newCell1 = newRow.insertCell(1);
  63.     var newCell2 = newRow.insertCell(2);
  64.     var newCell3 = newRow.insertCell(3);
  65.    
  66.     newCell0.innerHTML = author.value;
  67.     newCell1.innerHTML = project.value;
  68.     newCell2.innerHTML = cost.value;
  69.     newCell2.setAttribute('class', 'cost');
  70.  
  71. var btn = document.createElement("BUTTON");
  72.  
  73. var t = document.createTextNode("Remove");
  74. btn.appendChild(t);
  75. btn.setAttribute('onclick','removeRow(this);');
  76. newCell3.appendChild(btn);
  77.  
  78. author.value = "";
  79. project.value = "";
  80. cost.value = "";
  81. sumProjects();
  82. function removeRow(ref) {
  83.  
  84.     tr = ref.parentNode.parentNode;
  85.     tr.parentNode.removeChild(tr);
  86.     sumProjects();
  87.     }
  88.     function sumProjects() {
  89.     var costCells = document.getElementsByClassName('cost');
  90.     var sum = 0;
  91.     for(var i=0; i<costCells.length; i++) {
  92.     sum += parseInt(costCells[i].innerHTML);
  93.     }
  94.     document.getElementById('total').innerHTML = sum;
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment