Advertisement
Iv555

Untitled

Apr 6th, 2023
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. window.addEventListener('load', solve);
  2.  
  3. function solve() {
  4. const inputSelectors = {
  5. model: document.getElementById('model'),
  6. year: document.getElementById('year'),
  7. description: document.getElementById('description'),
  8. price: document.getElementById('price'),
  9. }
  10. const otherSelectors = {
  11. addBtn: document.getElementById('add'),
  12. form: document.getElementsByTagName('form')[0],
  13. tableBody: document.getElementById('furniture-list'),
  14. totalPriceTd: document.querySelector('#information tfoot .total-price'),
  15. }
  16. let totalProfit = 0.00;
  17. otherSelectors.addBtn.addEventListener('click', getFurnitureInfo);
  18.  
  19.  
  20. function getFurnitureInfo(event) {
  21. if (event) {
  22. event.preventDefault();
  23. }
  24. if (inputSelectors.model.value && inputSelectors.description.value && inputSelectors.year.value > 0 && inputSelectors.price.value > 0) {
  25. debugger;
  26. const model = inputSelectors.model.value;
  27. const year = inputSelectors.year.value;
  28. const description = inputSelectors.description.value;
  29. const price = parseFloat(inputSelectors.price.value).toFixed(2);
  30. const tableRowInfo = createElements('tr', '', false, otherSelectors.tableBody, '', ['info']);
  31. const modelTd = createElements('td', model, false, tableRowInfo);
  32. const priceTd = createElements('td', price, false, tableRowInfo);
  33. const actionsButtons = createElements('td', '', false, tableRowInfo);
  34. const moreBtn = createElements('button', 'More Info', false, actionsButtons, '', ['moreBtn']);
  35. const buyBtn = createElements('button', 'Buy it', false, actionsButtons, '', ['buyBtn']);
  36. const tableRowHide = createElements('tr', '', false, otherSelectors.tableBody, '', ['hide']);
  37. const yearTd = createElements('td', `Year: ${year}`, false, tableRowHide);
  38. const descriptionTd = createElements('td', `Description: ${description}`, false, tableRowHide, '', '', { colspan: "3" });
  39. otherSelectors.form.reset();
  40. moreBtn.addEventListener('click', () => {
  41. if (moreBtn.textContent === 'More Info') {
  42. moreBtn.textContent = 'Less Info';
  43. tableRowHide.style.display = 'contents';
  44.  
  45. }
  46. else {
  47. moreBtn.textContent = 'More Info';
  48. tableRowHide.style.display = 'none';
  49. }
  50. });
  51.  
  52. buyBtn.addEventListener('click', () => {
  53. tableRowInfo.remove();
  54. tableRowHide.remove();
  55. totalProfit = (Number(totalProfit) + Number(price)).toFixed(2);
  56. otherSelectors.totalPriceTd.textContent = totalProfit;
  57.  
  58. })
  59.  
  60. }
  61.  
  62.  
  63. }
  64. function createElements(type, contentOrValue, useInnerHTML, parentNode, id, classes, attributes) {
  65. const htmlElement = document.createElement(type);
  66. if (contentOrValue && useInnerHTML) {
  67. htmlElement.innerHTML = contentOrValue;
  68. }
  69. else {
  70. if (contentOrValue && type === 'input') {
  71. htmlElement.value = contentOrValue;
  72. }
  73. if (contentOrValue && type !== 'input') {
  74. htmlElement.textContent = contentOrValue;
  75. }
  76. }
  77.  
  78. if (id) {
  79. htmlElement.id = id;
  80. }
  81. if (classes) {
  82. htmlElement.classList.add(...classes)
  83. }
  84. // {src: 'link', href : 'http'}
  85. if (attributes) {
  86. for (const key in attributes) {
  87. htmlElement.setAttribute(key, attributes[key])
  88. }
  89. }
  90. if (parentNode) {
  91. parentNode.appendChild(htmlElement);
  92. }
  93. return htmlElement;
  94. }
  95.  
  96.  
  97. }
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement