your textI try to make todo app but when I click delete button it should delete element based on index but it does not delete the first element if I post once, but if I post multiple time then it deletes couple elements and how can I fix this?
Asked
Active
Viewed 35 times
-1
-
4Please, add your scripts as text not images so we can help you faster. – Ibrahim Hamaty Jun 03 '23 at 14:55
-
it did not allow me to add the code as the text therefore i screenshot them, somehow help me please – Beka Jun 03 '23 at 15:15
-
1Welcome to Stack Overflow! Please visit [help], take [tour] to see what and [ask]. Do some research, search for related topics on SO; if you get stuck, post a [mcve] of your attempt, noting input and expected output, preferably in a [Stacksnippet](https://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/) – mplungjan Jun 03 '23 at 15:44
-
Click [edit] then `[<>]` – mplungjan Jun 03 '23 at 15:44
1 Answers
0
I would change the calls a little like this:
<input class="url" />
<button onclick="addItem()">post</button>
<div class="content"></div>
<script>
let todos = [];
const input_value = document.querySelector(".url");
const postBtn = document.querySelector(".post-btn");
const content = document.querySelector(".content");
function addItem() {
let post = input_value.value;
todos.push(post);
renderTodos();
};
function renderTodos() {
content.innerHTML = "";
todos.map((todo, index) => {
let html = `<div class="el">
<p>${todo}</p>
<button class="del-btn" onclick=deletion('${index}')>DELETE</button>
</div>
`;
content.insertAdjacentHTML("afterbegin", html);
});
}
function deletion(key) {
delete todos[key];
renderTodos();
}
</script>
You were having problems with rendering after deletion. Your content.innerHTML = "";
in there was "replacing" the previous todos.

Gohchi
- 434
- 3
- 12