button function executes multiple times after the first time

I’m currently working on a todolist project and I’ve run into a problem adding new task to the project. When I submit new task for the first time, it worked fine and the task gets pushed into myProjects array. Next time I submit another task, it pushes multiple times of the same task into myProjects array.

Here’s my code:

const mainContent = document.querySelector('#main');



export default function renderMain(event) { //render project from sidebar to mainContent body



    mainContent.textContent = '';
    mainContent.appendChild(addTaskModal); //dialog to input new task and its priority
   
    let index = parseInt(event.target.id.split('-')[1]);

   //try creating the addTask button here.
    
    const projectCard = document.createElement('div');
    projectCard.classList.add('card');

    const projectCardHeader = document.createElement('h3');
    projectCardHeader.classList.add('cardheader');
    projectCardHeader.textContent =  myProjects[index].name
   projectCard.appendChild(projectCardHeader);    
   
   const projectDue = document.createElement('div');
    projectDue.textContent = myProjects[index].due;
    projectDue.classList.add('date');
    projectCard.appendChild(projectDue);

 

       ** submitNewTaskBtn.addEventListener('click', ()=>{
            
            if (!newTaskInput.value || !newPriorityInput.value) {
                alert('please enter the missing value.')
                return;
            }
            else if (myProjects[index].hasOwnProperty('tasks') === false) { //if project has no tasks property, assign tasks property
                myProjects[index].tasks =[(createTask(newTaskInput.value, newPriorityInput.value))];
                
                localStorage.setItem('projects', JSON.stringify(myProjects));
                addTaskModal.close();
                renderMain(event);
            }
            
            myProjects[index].tasks.push(createTask(newTaskInput.value, newPriorityInput.value));
            
            localStorage.setItem('projects', JSON.stringify(myProjects));
            
            addTaskModal.close();
           
            renderMain(event);
            
        })
    **
  
   for (let i = 0; i < myProjects[index].tasks?.length; i++) {
    
    const tasks = document.createElement('div');
   const deleteBtn = document.createElement('button');
   deleteBtn.textContent = 'Delete Task';
   deleteBtn.addEventListener('click', ()=>{
    myProjects[index].tasks.splice(i, 1);
    localStorage.setItem('projects', JSON.stringify(myProjects));
    renderMain(event);
   })
 
//when we want to update after delete, just store with same "key" and the "key" will be updated.
   
  
   tasks.textContent = myProjects[index].tasks[i].name;
   tasks.classList.add('tasks');
   tasks.appendChild(deleteBtn);
   projectCard.appendChild(tasks);
  
}
const addTaskBtn = document.createElement('button');
addTaskBtn.textContent = "Add Task";
mainContent.appendChild(addTaskBtn);
addTaskBtn.addEventListener('click', ()=>{
    
    addTaskModal.showModal();

});
    mainContent.appendChild(addTaskBtn);
    mainContent.appendChild(projectCard)

}


Specifically, this is the part of the code that I’m having a problem with:

  submitNewTaskBtn.addEventListener('click', ()=>{

            if (!newTaskInput.value || !newPriorityInput.value) {
                alert('please enter the missing value.')
                return;
            }
            else if (myProjects[index].hasOwnProperty('tasks') === false) { //if project has no tasks property, assign tasks property
                myProjects[index].tasks =[(createTask(newTaskInput.value, newPriorityInput.value))];

                localStorage.setItem('projects', JSON.stringify(myProjects));
                addTaskModal.close();
                renderMain(event);
            }

            myProjects[index].tasks.push(createTask(newTaskInput.value, newPriorityInput.value));

            localStorage.setItem('projects', JSON.stringify(myProjects));

            addTaskModal.close();

            renderMain(event);

        })


I’m not sure why it executes multiple times after the first submit, and the number of times doubles after every click.. 2,4,8,16 and so on. Any inputs would be very appreciated. Thank you!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật