JavaScript addEventListener not working after innerHTML

Sukima psychic ability's catched the main problem: your custom $ function (to replace document.getElementById) used a caching mechanism.

After some testing (out of personal curiosity) it turned out that as long as you have some reference to an element, the element is still accessible, even after elm.parentNode.removeChild or a full elm.parentNode.innerHTML rewrite (at least, in FF).

In other words: the events WERE added, every time, just to the wrong/old/previous elements instead of the new ones. Otherwise there would also have been errors that the elements didn't exist and thus didn't have an addEventListener method..

See this crude test-fiddle for proof: http://jsfiddle.net/G28Lu/


I toyed around with a $ function (as you haven't posted yours yet) and gave it an 'refresh'-flag to go with the optional cache mechanism:

var $=function(/*cache_enabled*/c){  // getElementById with optional caching & refresh
   return c ?( c={}, function(s,r){return (!r && c[s]) || (c[s]=document.getElementById(s));} 
           ):( function(s){return document.getElementById(s);} );
}(true); // or nothing or 0 etc to disable the cache

Note: dynamically created functions are slow (for some reason) so it has 2 functions so the browser can run them optimized (and the other should be cleared by the garbage collector).

To request a fresh just add a flag that evaluates to true as second argument, like: $('elm_id', 1)

Then I modified your addMainMenuListeners function a little to first test for the existence of a fresh getElementById and then add the eventListener via the freshly updated cached reference (so, essentially I changed nothing in the flow of your routine).

formatted to emphasize what changed and how it works

function addMainMenuListeners(){
   $(   'start'    , 1) && $(   'start'    ).addEventListener('click', startGame);
   $('instructions', 1) && $('instructions').addEventListener('click', instructions);
   $(  'settings'  , 1) && $(  'settings'  ).addEventListener('click', settings);
   $(   'back'     , 1) && $(    'back'    ).addEventListener('click', resetMainMenu);
   $(  'volume'    , 1) && $(   'volume'   ).addEventListener('change', function(){ 
                                               saveAndChangeVolume($('volume').value); 
                                             });
}

Finally, putting above 2 functions and the rest of your functions/html into this fiddle rendered a working result: http://jsfiddle.net/xHUGu/ !
Note: I had to substitute a dummy function startGame otherwise there would be a fatal error. The missing volume-functions were not critical.

I would like to point out that this is not really the way to go with your interface, there would be a lot of work you could save both yourself and the browser. You might want to look into div's (containing your subsections of html) and toggle them so there is only one visible. Like tabs. (Hint for google-query).

Credit still goes to Sukima ('the force is strong in this one'), but I felt it was a good idea to share the correct explanation to your problem (with proof) and not to waste the work that was done anyway (out of curiosity).

Hope this helps!

Answer:1

I need your help, if the following code below counts every table row in a table, how can it be amended such that the code won't count the <th></th> as a row itself? var rows = document....

I need your help, if the following code below counts every table row in a table, how can it be amended such that the code won't count the <th></th> as a row itself? var rows = document....

  1. counting number rows sql
  2. counting rows number
  3. counting number of rows in excel
  4. counting number of rows in r
  5. counting number of rows in python
  6. counting number of rows in pandas
  7. counting number of rows in dataframe
  8. counting number of rows in pandas dataframe
  9. counting number of rows in excel vba
  10. counting number of rows in vba
  11. counting number of rows in mysql
  12. counting number of rows in csv python
  13. counting number of rows in sas
  14. counting number of rows in php
  15. counting number of rows in excel after filter
  16. counting number of rows in a range vba
  17. counting number of rows in excel using vba
  18. counting number of rows in dataframe r
  19. counting number of rows in hive
  20. counting number of rows in a table

I'm trying to access an object that I retrieve from MongoDB with my client-side JS. Specifically, I want to be able to loop through and use the arrays within the object. Here is my server-side JS ...

I'm trying to access an object that I retrieve from MongoDB with my client-side JS. Specifically, I want to be able to loop through and use the arrays within the object. Here is my server-side JS ...

  1. send object from one activity to another
  2. send object from activity to fragment
  3. send object from one fragment to another
  4. send object from jsp to servlet
  5. send object from jsp to controller
  6. send object from postman
  7. send object from view to controller
  8. send object from one component to another angular
  9. send object from angular to spring boot
  10. send object from client to server java
  11. send object from ajax to controller
  12. send object from php to javascript
  13. send object from child to parent angular

I want to update a clock in the UI when the time changes from 12:01 to 12:02. I can do a setInterval every 60 seconds, but the beginning might not be on the first second of a new minute. How can I ...

I want to update a clock in the UI when the time changes from 12:01 to 12:02. I can do a setInterval every 60 seconds, but the beginning might not be on the first second of a new minute. How can I ...

Using the following code: params = { Bucket: 'bucket.com', Key: zipPath }, s3 = new AWS.S3(); params.Body = zipFile; s3.putObject(params, function(err, data) { if (err) { ...

Using the following code: params = { Bucket: 'bucket.com', Key: zipPath }, s3 = new AWS.S3(); params.Body = zipFile; s3.putObject(params, function(err, data) { if (err) { ...

  1. upload file amazon s3 javascript
  2. upload file amazon s3 java
  3. upload file amazon s3 android
  4. upload file amazon s3 c#
  5. upload file amazon s3
  6. upload file amazon s3 command line
  7. upload file amazon s3 using php
  8. upload file amazon cloud drive
  9. upload amazon file
  10. upload file to amazon s3 codeigniter
  11. upload file to amazon s3 node js
  12. upload file to amazon workspace
  13. upload file to amazon s3 laravel
  14. upload file to amazon s3 bucket c#
  15. upload file to amazon s3 php
  16. upload file to amazon ec2
  17. upload file to amazon ec2 instance
  18. upload file to amazon s3 python
  19. upload flat file amazon
  20. upload file to amazon kindle