I am new in Javascript and to get the hang of it i am trying to make a 2 do list where the end goal is to create a drag and drop list with my to2dos.
My problem is that when i click the + button that adds divs to the page it only creates delete buttons in the first div.
I searched for some hours on this forum and google could not find an answer and have been struggled for hours ... :-)
I hope you guys can give me a hint. Thanks!
HTML code:
<body>
<header class="header">
<h1>The Drag&Drop2Do</h1>
</header>
<section class="worksection">
<div class="workrow">
<div class="workheader">
<p>Dagens opgaver</p>
</div>
</div>
<div class="workrow">
<div class="workheader">
<p>Færdige opgaver</p>
</div>
</div>
<div class="workrow">
<div class="workheader">
<p>Udskudte opgaver</p>
</div>
</div>
<div class="workrow">
<div class="workheader">
<p>Afsluttet opgaver</p>
</div>
</div>
</section>
<div class="buttonwrapper">
<button id="btn">+</button>
</div>
<script src="./javascript/script.js"></script>
CSS code (not all ofcourse :-) ):
.workrowtask {
width: 100%;
background-color: gray;
padding: 10px 0px;
margin-top: 10px;
display: flex;
flex-wrap: nowrap;
justify-content: space-around;
}
.worktaskdelbtn {
background-color: red;
color: white;
height: 15px;
width: 15px;
border-radius: 50%;
border: none;
cursor: pointer;
outline: none;
}
Javascript:
const btn = document.querySelector('#btn');
const workRow = document.querySelector('.workrow');
btn.addEventListener('click', makePost);
function makePost() {
const div = document.createElement('div');
const textnode = document.createTextNode('Hello World');
div.appendChild(textnode);
div.setAttribute('draggable', true);
div.className += ('workrowtask');
workRow.appendChild(div);
const workrowtask = document.querySelector('.workrowtask');
const delbtn = document.createElement('button');
const textnodebtn = document.createTextNode('-');
delbtn.appendChild(textnodebtn);
delbtn.className += ('worktaskdelbtn');
workrowtask.appendChild(delbtn);
};
Here is a JSBin to show the delete buttons https://jsbin.com/xezoxitari/edit?html,css,js,output
Drag&Drop2Dois invalid HTML. You need to writeDrag&Drop2Do.