Некорректно работает событие drop при DnD vanilaJS

Делаю простую приложуху по учебе, аналог трелло, в рамках которой перенос задач реализован через dnd api. Не понимаю, почему при срабатывании события drop в другую калонку вместо задачи просто переносится null.

const deleteButton = document.querySelectorAll('.column__card-button');
const addButton = document.querySelector('.add_card')
const cardContent = document.querySelector('.column__card-content_add')
const columns = document.querySelectorAll('.column')
const columnTitle = document.querySelector('.column_title')
const mainContainer = document.querySelector('.main_container')
let actualCard = null;
const tasks = document.querySelectorAll('.column__card')

addButton.addEventListener('click', function() {
  const newColumnCard = document.createElement('div');
  const randomId = 'card-' + Math.random().toString(36).substring(2, 9);
  columnTitle.insertAdjacentHTML("afterEnd", `<div class="column__card" draggable="true" id="${randomId}"><div class="column__card-content">Здесь будут прописаны задачи</div><button class="column__card-button"></button></div>`);
});


mainContainer.addEventListener('click', function(event) {
  if (event.target.classList.contains('column__card-button')) {
    const card = event.target.closest('.column__card');
    if (card) {
      card.remove();
    }
  }
});

function dragStart(e) {
  e.dataTransfer.setData('text/plain', e.target.getAttribute('id'));
  this.classList.add("is-dragging");
  console.log(this)
};

function dragEnd(e) {
  this.classList = 'column__card';
  console.log(this)
};

function dragEnter(e) {
  e.preventDefault();
  this.classList.add('hovered');
};

function dragLeave() {
  this.classList.remove('hovered');
};

function dragOver(e) {
  e.preventDefault();
};

tasks.forEach(task => {
  task.addEventListener('dragstart', dragStart);
  task.addEventListener('dragend', dragEnd);
});

for (const column of columns) {
  column.addEventListener('dragenter', dragEnter);
  column.addEventListener('dragleave', dragLeave);
  column.addEventListener('dragover', dragOver);
  column.addEventListener('drop', drop);
}

function drop(e) {
  e.preventDefault();
  this.classList = 'column'
  const id = e.dataTransfer.getData('text/plain');
  console.log(id)
  e.target.append(document.getElementById(id))
};
body {
  background: rgb(122, 192, 255);
  background: linear-gradient(0deg, rgba(122, 192, 255, 1) 0%, rgba(48, 163, 255, 1) 54%, rgba(0, 138, 255, 1) 100%);
}

.main_container {
  display: flex;
  justify-content: space-between;
  margin: 0 auto;
  max-width: 1440px
}

.column {
  background-color: #DDDFE7;
  margin: 0 auto;
  border-radius: 4px;
  min-width: 436px;
  min-height: 350px;
}

.column_title {
  margin: 16px;
}

.column__card {
  display: flex;
  background-color: white;
  max-width: 405px;
  margin-left: 16px;
  border-radius: 3px;
  justify-content: space-between;
  margin-bottom: 140px;
}

.adding_card {
  display: flex;
  justify-content: space-between;
}

.column__card-content {
  margin: 16px;
}

.column__card-button {
  min-width: 25px;
  height: 25px;
  box-shadow: 0px 10 10px 10px rgba(0, 0, 0, 0.25);
  border-radius: 6px;
  background: #3379b77a;
  margin: 10px;
  position: relative;
  display: block;
  z-index: 200;
  text-indent: -9999px;
  cursor: pointer;
}

.column__card-button:before,
.column__card-button:after {
  content: '';
  width: 80%;
  height: 2px;
  background: #ffffff;
  position: absolute;
  top: 48%;
  left: 10%;
  transform: rotate(45deg);
  transition: all 0.3s ease-out;
}

.column__card-button:after {
  transform: rotate(-45deg);
  transition: all 0.3s ease-out;
}

.column__card-button:hover:before,
.column__card-button:hover:after {
  transform: rotate(180deg);
  background: #FFF;
}

.column__card-content_add {
  margin: 10px;
  min-width: 250px;
  min-height: 65px;
  max-width: 300px;
  max-height: 100px;
}

.add_card {
  align-items: center;
  appearance: button;
  background-color: #0276FF;
  border-radius: 8px;
  border-style: none;
  box-shadow: rgba(255, 255, 255, 0.26) 0 1px 2px inset;
  box-sizing: border-box;
  color: #fff;
  cursor: pointer;
  display: flex;
  flex-direction: row;
  flex-shrink: 0;
  font-family: "RM Neue", sans-serif;
  font-size: 100%;
  line-height: 1.15;
  margin: 0;
  padding: 10px 21px;
  text-align: center;
  text-transform: none;
  transition: color .13s ease-in-out, background .13s ease-in-out, opacity .13s ease-in-out, box-shadow .13s ease-in-out;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  max-width: 130px;
  max-height: 35px;
  margin: 10px;
}

.add_card:active {
  background-color: #006AE8;
}

.add_card:hover {
  background-color: #1C84FF;
}

.is-dragging {
  opacity: 0.5;
  transform: translate3d(0, 0, 0);
  transition: transform 0ms, opacity 0ms;
  cursor: grabbing;
  z-index: 1000;
}

.column__card:hover {
  cursor: grab;
}
<div class="main_container">
  <div class="column">
    <h3 class="column_title">ToDo</h3>
    <div class="column__card" draggable="true">
      <div class="column__card-content">Вот тут будут прописаны задачи</div>
      <button class="column__card-button"></button>
    </div>
    <div class="adding_card">
      <textarea class="column__card-content_add" autocomplete="on" placeholder="Запишите задачу"></textarea>
      <button class="add_card">Add card</button>
    </div>
  </div>
  <div class="column">
    <h3 class="column_title">In progress</h3>
  </div>
  <div class="column">
    <h3 class="column_title">Done</h3>
  </div>
</div>


Ответы (1 шт):

Автор решения: Arbery

Потому что id у вас у карточки нету и вот это бесполезно e.target.getAttribute('id') . В вашем случае достаточно добавлять id нужному элементу:

<div class="column__card" draggable="true" id='el1'></div>

const deleteButton = document.querySelectorAll('.column__card-button');
const addButton = document.querySelector('.add_card')
const cardContent = document.querySelector('.column__card-content_add')
const columns = document.querySelectorAll('.column')
const columnTitle = document.querySelector('.column_title')
const mainContainer = document.querySelector('.main_container')
let actualCard = null;
const tasks = document.querySelectorAll('.column__card')

addButton.addEventListener('click', function() {
  const newColumnCard = document.createElement('div');
  const randomId = 'card-' + Math.random().toString(36).substring(2, 9);
  columnTitle.insertAdjacentHTML("afterEnd", `<div class="column__card" draggable="true" id="${randomId}"><div class="column__card-content">Здесь будут прописаны задачи</div><button class="column__card-button"></button></div>`);
});


mainContainer.addEventListener('click', function(event) {
  if (event.target.classList.contains('column__card-button')) {
    const card = event.target.closest('.column__card');
    if (card) {
      card.remove();
    }
  }
});

function dragStart(e) {
  e.dataTransfer.setData('text/plain', e.target.getAttribute('id'));
  this.classList.add("is-dragging");
  console.log(this)
};

function dragEnd(e) {
  this.classList = 'column__card';
  console.log(this)
};

function dragEnter(e) {
  e.preventDefault();
  this.classList.add('hovered');
};

function dragLeave() {
  this.classList.remove('hovered');
};

function dragOver(e) {
  e.preventDefault();
};

tasks.forEach(task => {
  task.addEventListener('dragstart', dragStart);
  task.addEventListener('dragend', dragEnd);
});

for (const column of columns) {
  column.addEventListener('dragenter', dragEnter);
  column.addEventListener('dragleave', dragLeave);
  column.addEventListener('dragover', dragOver);
  column.addEventListener('drop', drop);
}

function drop(e) {
  e.preventDefault();
  this.classList = 'column'
  const id = e.dataTransfer.getData('text/plain');
  console.log(id)
  e.target.append(document.getElementById(id))
};
body {
  background: rgb(122, 192, 255);
  background: linear-gradient(0deg, rgba(122, 192, 255, 1) 0%, rgba(48, 163, 255, 1) 54%, rgba(0, 138, 255, 1) 100%);
}

.main_container {
  display: flex;
  justify-content: space-between;
  margin: 0 auto;
  max-width: 1440px
}

.column {
  background-color: #DDDFE7;
  margin: 0 auto;
  border-radius: 4px;
  min-width: 436px;
  min-height: 350px;
}

.column_title {
  margin: 16px;
}

.column__card {
  display: flex;
  background-color: white;
  max-width: 405px;
  margin-left: 16px;
  border-radius: 3px;
  justify-content: space-between;
  margin-bottom: 140px;
}

.adding_card {
  display: flex;
  justify-content: space-between;
}

.column__card-content {
  margin: 16px;
}

.column__card-button {
  min-width: 25px;
  height: 25px;
  box-shadow: 0px 10 10px 10px rgba(0, 0, 0, 0.25);
  border-radius: 6px;
  background: #3379b77a;
  margin: 10px;
  position: relative;
  display: block;
  z-index: 200;
  text-indent: -9999px;
  cursor: pointer;
}

.column__card-button:before,
.column__card-button:after {
  content: '';
  width: 80%;
  height: 2px;
  background: #ffffff;
  position: absolute;
  top: 48%;
  left: 10%;
  transform: rotate(45deg);
  transition: all 0.3s ease-out;
}

.column__card-button:after {
  transform: rotate(-45deg);
  transition: all 0.3s ease-out;
}

.column__card-button:hover:before,
.column__card-button:hover:after {
  transform: rotate(180deg);
  background: #FFF;
}

.column__card-content_add {
  margin: 10px;
  min-width: 250px;
  min-height: 65px;
  max-width: 300px;
  max-height: 100px;
}

.add_card {
  align-items: center;
  appearance: button;
  background-color: #0276FF;
  border-radius: 8px;
  border-style: none;
  box-shadow: rgba(255, 255, 255, 0.26) 0 1px 2px inset;
  box-sizing: border-box;
  color: #fff;
  cursor: pointer;
  display: flex;
  flex-direction: row;
  flex-shrink: 0;
  font-family: "RM Neue", sans-serif;
  font-size: 100%;
  line-height: 1.15;
  margin: 0;
  padding: 10px 21px;
  text-align: center;
  text-transform: none;
  transition: color .13s ease-in-out, background .13s ease-in-out, opacity .13s ease-in-out, box-shadow .13s ease-in-out;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  max-width: 130px;
  max-height: 35px;
  margin: 10px;
}

.add_card:active {
  background-color: #006AE8;
}

.add_card:hover {
  background-color: #1C84FF;
}

.is-dragging {
  opacity: 0.5;
  transform: translate3d(0, 0, 0);
  transition: transform 0ms, opacity 0ms;
  cursor: grabbing;
  z-index: 1000;
}

.column__card:hover {
  cursor: grab;
}
<div class="main_container">
  <div class="column">
    <h3 class="column_title">ToDo</h3>
    <div class="column__card" draggable="true" id='el1'>
      <div class="column__card-content">Вот тут будут прописаны задачи</div>
      <button class="column__card-button"></button>
    </div>
    <div class="adding_card">
      <textarea class="column__card-content_add" autocomplete="on" placeholder="Запишите задачу"></textarea>
      <button class="add_card">Add card</button>
    </div>
  </div>
  <div class="column">
    <h3 class="column_title">In progress</h3>
  </div>
  <div class="column">
    <h3 class="column_title">Done</h3>
  </div>
</div>

Или более универсальный вариант без привязки к id, чтобы не назначать каждому новому элементу: При dragStart

actualCard = e.target

а потом перекидывать его при drop в целевой элемент

e.target.appendChild(actualCard);

В рабочем виде так:

const deleteButton = document.querySelectorAll('.column__card-button');
const addButton = document.querySelector('.add_card')
const cardContent = document.querySelector('.column__card-content_add')
const columns = document.querySelectorAll('.column')
const columnTitle = document.querySelector('.column_title')
const mainContainer = document.querySelector('.main_container')
let actualCard = null;
const tasks = document.querySelectorAll('.column__card')

addButton.addEventListener('click', function() {
  const newColumnCard = document.createElement('div');
  const randomId = 'card-' + Math.random().toString(36).substring(2, 9);
  columnTitle.insertAdjacentHTML("afterEnd", `<div class="column__card" draggable="true" id="${randomId}"><div class="column__card-content">Здесь будут прописаны задачи</div><button class="column__card-button"></button></div>`);
});


mainContainer.addEventListener('click', function(event) {
  if (event.target.classList.contains('column__card-button')) {
    const card = event.target.closest('.column__card');
    if (card) {
      card.remove();
    }
  }
});

function dragStart(e) {
  actualCard = e.target
  this.classList.add("is-dragging");
  console.log(this, 'start')
};

function dragEnd(e) {
  this.classList = 'column__card';
  console.log(this, 'end')
};

function dragEnter(e) {
  e.preventDefault();
  this.classList.add('hovered');
};

function dragLeave() {
  this.classList.remove('hovered');
};

function dragOver(e) {
  e.preventDefault();
};

tasks.forEach(task => {
  task.addEventListener('dragstart', dragStart);
  task.addEventListener('dragend', dragEnd);
});

for (const column of columns) {
  column.addEventListener('dragenter', dragEnter);
  column.addEventListener('dragleave', dragLeave);
  column.addEventListener('dragover', dragOver);
  column.addEventListener('drop', drop);
}


function drop(e) {
  e.preventDefault();
  e.target.appendChild(actualCard);
};
body {
  background: rgb(122, 192, 255);
  background: linear-gradient(0deg, rgba(122, 192, 255, 1) 0%, rgba(48, 163, 255, 1) 54%, rgba(0, 138, 255, 1) 100%);
}

.main_container {
  display: flex;
  justify-content: space-between;
  margin: 0 auto;
  max-width: 1440px
}

.column {
  background-color: #DDDFE7;
  margin: 0 auto;
  border-radius: 4px;
  min-width: 436px;
  min-height: 350px;
}

.column_title {
  margin: 16px;
}

.column__card {
  display: flex;
  background-color: white;
  max-width: 405px;
  margin-left: 16px;
  border-radius: 3px;
  justify-content: space-between;
  margin-bottom: 140px;
}

.adding_card {
  display: flex;
  justify-content: space-between;
}

.column__card-content {
  margin: 16px;
}

.column__card-button {
  min-width: 25px;
  height: 25px;
  box-shadow: 0px 10 10px 10px rgba(0, 0, 0, 0.25);
  border-radius: 6px;
  background: #3379b77a;
  margin: 10px;
  position: relative;
  display: block;
  z-index: 200;
  text-indent: -9999px;
  cursor: pointer;
}

.column__card-button:before,
.column__card-button:after {
  content: '';
  width: 80%;
  height: 2px;
  background: #ffffff;
  position: absolute;
  top: 48%;
  left: 10%;
  transform: rotate(45deg);
  transition: all 0.3s ease-out;
}

.column__card-button:after {
  transform: rotate(-45deg);
  transition: all 0.3s ease-out;
}

.column__card-button:hover:before,
.column__card-button:hover:after {
  transform: rotate(180deg);
  background: #FFF;
}

.column__card-content_add {
  margin: 10px;
  min-width: 250px;
  min-height: 65px;
  max-width: 300px;
  max-height: 100px;
}

.add_card {
  align-items: center;
  appearance: button;
  background-color: #0276FF;
  border-radius: 8px;
  border-style: none;
  box-shadow: rgba(255, 255, 255, 0.26) 0 1px 2px inset;
  box-sizing: border-box;
  color: #fff;
  cursor: pointer;
  display: flex;
  flex-direction: row;
  flex-shrink: 0;
  font-family: "RM Neue", sans-serif;
  font-size: 100%;
  line-height: 1.15;
  margin: 0;
  padding: 10px 21px;
  text-align: center;
  text-transform: none;
  transition: color .13s ease-in-out, background .13s ease-in-out, opacity .13s ease-in-out, box-shadow .13s ease-in-out;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  max-width: 130px;
  max-height: 35px;
  margin: 10px;
}

.add_card:active {
  background-color: #006AE8;
}

.add_card:hover {
  background-color: #1C84FF;
}

.is-dragging {
  opacity: 0.5;
  transform: translate3d(0, 0, 0);
  transition: transform 0ms, opacity 0ms;
  cursor: grabbing;
  z-index: 1000;
}

.column__card:hover {
  cursor: grab;
}
<div class="main_container">

  <div class="column">
    <h3 class="column_title">ToDo</h3>
    <div class="column__card" draggable="true">
      <div class="column__card-content">Вот тут будут прописаны задачи</div>
      <button class="column__card-button"></button>
    </div>
    <div class="adding_card">
      <textarea class="column__card-content_add" autocomplete="on" placeholder="Запишите задачу"></textarea>
      <button class="add_card">Add card</button>
    </div>
  </div>
  <div class="column">
    <h3 class="column_title">In progress</h3>
  </div>
  <div class="column">
    <h3 class="column_title">Done</h3>
  </div>
</div>

→ Ссылка