-1

I used document.createElement('div') to create a div. The element <i> is not in HTML, it's only created via JavaScript once the submit button is pressed as shown in the script. How can I fix this error so that when the button is pressed and the element shows up, I can click it to delete the divBox?

(the <i> element is basically delete button that once it's pressed it deletes the divBox)

const deleteBtn = document.querySelector(".fa-delete-left");
const divBox = document.querySelector(".divBox");

submit.addEventListener("click", () => {
  let message = document.createElement("div");

  if (userInput.value == "") {
    return false;
  } else {
    message.innerHTML = `<div class="divBox"></p>${userInput.value}
    <i class="fa-solid fa-delete-left"></i><p>${month} ${day} , ${year}</div>`;
  }
  div.box.append(message)
});

deleteItem.addEventListener("click", () => {
  divBox.remove();
});
Barmar
  • 741,623
  • 53
  • 500
  • 612
mDR
  • 9
  • 2
  • 2
    Is this any different from the quesiton you asked earlier? https://stackoverflow.com/questions/76877893/how-to-use-dom-to-select-an-element-that-is-not-in-html-but-created-in-js-using Didn't you see how I edited it so that the `` elements would show up in the text? – Barmar Aug 11 '23 at 01:42
  • 1
    you need to put `const divBox = document.querySelector(".divBox");` inside the event listener. The divbox doesn't exist when the page is first loaded. – Barmar Aug 11 '23 at 01:43
  • `
    ` — what is the `` for? It does not make any sense.
    – Pointy Aug 11 '23 at 01:44
  • also `div.box.append` — what is `div.box`? – Pointy Aug 11 '23 at 01:45
  • Looks like he swapped `

    ` and `

    ` when transcribing. And `div.box` is obvioiusly a typo for `divBox`.
    – Barmar Aug 11 '23 at 01:47
  • Stack Overflow is not a forum, and if you make edits to a closed question to improve it, it will be reviewed by more experienced users and potentially reopened. Do not post the same question twice to try and work around the site's review process, that's akin to spamming and it just makes the site worse for everyone. – Domino Aug 11 '23 at 02:18

0 Answers0