0

Duplicate of If a DOM Element is removed, are its listeners also removed from memory?


Why jQuery's .empty() off all events on the elements who gotten removed?

If I am using vanilla JavaScript, is removing event listeners when elements removed from document should be considered as a good practice? nowadays. That's all.

tsh
  • 4,263
  • 5
  • 28
  • 47
  • Read up on **[event delegation](http://learn.jquery.com/events/event-delegation/)** – charlietfl May 24 '17 at 11:41
  • @charlietfl you may say one solution should be event delegation. but this question is asking about why jquery try to remove events when empty is called. i do not think that question had answered this. – tsh May 25 '17 at 01:04
  • @charlietfl and i do not see much benifit from event delegation. get Element object and set innerHTML directly just simply solved my bug, it works with only one line of modification. though it is ugly. – tsh May 25 '17 at 01:08
  • Do not use `empty()` but use `innerHTML = ""` so events will not be stripped away. – vsync Jul 13 '17 at 08:12
  • google gives tons of information, why did you open this question before googling? – vsync Jul 13 '17 at 08:14
  • duplicate - https://stackoverflow.com/q/11609053/104380 – vsync Jul 13 '17 at 08:15
  • duplicate - https://stackoverflow.com/q/8120441/104380 – vsync Jul 13 '17 at 08:15
  • @vsync I know how to use `.empty()` in jQuery. And I know they do remove event handlers. But I'm asking the benefit from doing so, and whether I should do the same when using vanilla js. Had any questions or answers you linked mentioned this? – tsh Jul 13 '17 at 08:25
  • You did not ask such in your title. please write concise titles next time with the exact question or problem you are facing instead of some generalized title. Anyway, you should go over all the google results and read everything, I only know there are already *plenty* of discussions about this topic. – vsync Jul 13 '17 at 08:40
  • and you should read books about javascript before asking complex questions or simply google... https://stackoverflow.com/q/10092619/104380 – vsync Jul 13 '17 at 08:43
  • So should not the word "WHY" in the title means that? – tsh Jul 13 '17 at 08:43
  • @vsync I had read books about JavaScript, and also specifications about JavaScript. But I do not think this had some contribute to this question. And there is no means one cannot ask a question on SO if one can know it from some book. You had pointed me a link which discuss memory leak. But I do not think one should know keywords like "memory leak" for searching before asking this question. Also, one may say "memory leaking" is one reason here. But one should not guarantee that is the only one. – tsh Jul 13 '17 at 08:58
  • @tsh - you need to learn about javascript first, which is a world of content, and then learn the DOM, which is also a vast world of knowledge to be learnt. once you learn the basics, one should know about memory leaks and circular reference, those are basic stuff. you might claim it is not basic, but I would argue it is quite so. – vsync Jul 13 '17 at 11:22
  • in my days, before this website existed, I would google for hours and learn by sheer will-power, but today people are expected to be fed with instant answers by others who did spent their time studying thoroughly. please respect people's time before giving up on google and really coming to a dead-end – vsync Jul 13 '17 at 11:25
  • 1. Why "circular reference" cause memory leak? It is so reasonable? 2. If you think it is wast of time to answer such basic questions, you may leave it to others. That's up to you. Asking a question here does help people with same question get the answer more easily, since they may use some search engine and come here. And that's why SO is here. You may prefer search for hours to study the cause, but that not means it _should_. – tsh Jul 14 '17 at 01:11

0 Answers0