0
window.addEventListener("mouseup", function(event) {
  let boxen = document.getElementById("box")
  if (typeof event !== boxen) {
    boxen.remove()
  }
})

Image of my code:

image of my code

Rory O'Kane
  • 29,210
  • 11
  • 96
  • 131
  • 3
    Do you have an element with ID `box` in your page? – VLAZ Jun 03 '19 at 11:21
  • 1
    Also, most likely related: https://stackoverflow.com/questions/8739605/getelementbyid-returns-null – VLAZ Jun 03 '19 at 11:22
  • @VLAZ yes, i have – Jasmine Löfgren Jun 03 '19 at 11:27
  • My guess would be, that the DOM isn't ready yet. Try wrapping your code inside an event listener that listens to DOMContentLoaded. See: https://developer.mozilla.org/de/docs/Web/Events/DOMContentLoaded – Tom Jun 03 '19 at 11:30
  • 2
    @JasmineLöfgren Not at the point that you are executing the `getElementById` according to the returned `null`. Is the box element always there or is it added later? Or is it removed at some point? Please add a [mcve] to your question. Without more information it's impossible for us to answer. – Ivar Jun 03 '19 at 11:33
  • @Ivar getElementById("box") is created in js first, then i have if getElementById("box") { getElementById("box").remove(), because when I click in an another position , the previous box should disappear... this is my first time with js haha – Jasmine Löfgren Jun 03 '19 at 11:46
  • 1
    @JasmineLöfgren That is understandable, we all started at some point. It seems that you call that line either before it has been created or after it has been removed. But without a [mcve] we don't have the information to exactly tell you what is wrong. – Ivar Jun 03 '19 at 11:57

0 Answers0