Javascript ToDo list

Could you please check my todolist(still not ready) : https://codepen.io/ls9690/pen/poKMEEJ

Every time I click on the “>>” button on the right side it clears the div container and adds the testcontent of the last project added to the array.
I want to click on the ‘>>’ button and add the project that I clicked on, not always the last I added.

could someone help me. I just tried everything. I can’t solve this issue to go on…

That’s because you’re hardcoding the item to show:

  div.textContent = projectArray[projectArray.length - 1].name;
  div.setAttribute("data-id", projectArray[projectArray.length - 1].id);

projectArray[projectArray.length - 1] will always select the last item from the array.

Simplest way (not the best though) would be to check which button was clicked:

function displayTasks(e) { // Add event as an argument
  const id = e.target.parentElement.dataset.id // Get id of the button wrapping div. This won't work if you change your HTML
  const project = projectArray.find(p => p.id == id) // use this to get id and name

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.