Advertisement
Silviya7

2.Expense Tracker

Apr 12th, 2024 (edited)
1,094
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. window.addEventListener("load", solve);
  2.  
  3. //TODO
  4.  
  5. function solve(){
  6.  
  7.  
  8.     const BtnAdd= document.getElementById('add-btn');
  9.     const UlList= document.getElementById('preview-list');
  10.     const UlListExpense= document.getElementById('expenses-list');
  11.  
  12.     const BtnDelete= document.querySelector('#expenses .btn.delete');
  13.  
  14.     //input fields
  15.    const ElExp=document.getElementById('expense');
  16.    const Elamount=document.getElementById('amount');
  17.    const Eldate= document.getElementById('date');
  18.  
  19.    BtnAdd.addEventListener('click',()=>{
  20.  
  21.     if(ElExp.value=='' ||   Elamount.value=='' ||   Eldate.value==''){
  22.         return;
  23.     }
  24.  
  25.     const Elarticle=document.createElement('article');
  26.     const elp1=document.createElement('p');
  27.     elp1.textContent=`Type: ${ElExp.value}`;
  28.  
  29.     const elp2=document.createElement('p');
  30.     elp2.textContent=`Amount: ${Elamount.value}$`;
  31.     const elp3=document.createElement('p');
  32.     elp3.textContent=`Date: ${Eldate.value}`;
  33.  
  34.     Elarticle.appendChild(elp1);
  35.     Elarticle.appendChild(elp2);
  36.     Elarticle.appendChild(elp3);
  37.  
  38.     const btnEdit=document.createElement('button');
  39.     btnEdit.classList.add('btn', 'edit')
  40.     btnEdit.textContent='edit';
  41.  
  42.     const BtnOk=document.createElement('button');
  43.     BtnOk.classList.add('btn', 'ok')
  44.     BtnOk.textContent='ok';
  45.  
  46.  
  47.     const ContainerBtns=document.createElement('div');
  48.     ContainerBtns.classList.add('buttons');
  49.    
  50.     ContainerBtns.appendChild(btnEdit);
  51.     ContainerBtns.appendChild(BtnOk);
  52.  
  53.     const LiEl= document.createElement('li');
  54.      LiEl.classList.add('expense-item');
  55.     LiEl.appendChild(Elarticle);
  56.     LiEl.appendChild(ContainerBtns);
  57.  
  58.     UlList.appendChild(LiEl);
  59.     BtnAdd.setAttribute('disabled', 'disabled');
  60.  
  61.     ElExp.value='' ;
  62.     Elamount.value='' ;
  63.     Eldate.value='';
  64.  
  65.     btnEdit.addEventListener('click',()=>{
  66.  
  67.         let ListNodes= document.querySelectorAll('article p');
  68.         let AllList=Array.from(ListNodes);
  69.  
  70.         ElExp.value=elp1.textContent.substring(6);
  71.         Elamount.value=elp2.textContent.substring(8,AllList[1].textContent.length-1);
  72.         Eldate.value=elp3.textContent.substring(6);
  73.  
  74.         UlList.removeChild(LiEl);
  75.         BtnAdd.removeAttribute('disabled');
  76.     })
  77.  
  78.     BtnOk.addEventListener('click',()=>{
  79.  
  80.         UlList.removeChild(LiEl);
  81.         UlListExpense.appendChild(LiEl);
  82.         LiEl.removeChild(ContainerBtns);
  83.         BtnAdd.removeAttribute('disabled');
  84.     })
  85.  
  86.  
  87.    })
  88.  
  89.    BtnDelete.addEventListener('click',()=>{
  90.     UlList.innerHTML='';
  91.     UlListExpense.innerHTML='';
  92.     })
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement