dbawel Posted June 18, 2018 Share Posted June 18, 2018 Hello, I've tried every location and jquery syntax for hours, and I still get an error: Uncaught TypeError: Cannot read property 'addEventListener' of null at main2.js:47. It's simply not finding the ID of the variable I've set. Any help is appriciated. It's 3am, so I'm sure I'll feel stupid in the morning. http://qedsoft.com/DEMOS2017/mobile_app/index2.html Thanks, DB Quote Link to comment Share on other sites More sharing options...
RaananW Posted June 18, 2018 Share Posted June 18, 2018 Loading the script in the head element will call the javascript before the DOM war generated. hence - the dom elements are null. if you use jquery, you can wrap it in a DOM-Ready function: $(function() { // code goes here }); but you can simply add the javascript at the bottom of the body tag, and it will be executed after the dom was loaded (if you dont create the DOM dynamically) dbawel 1 Quote Link to comment Share on other sites More sharing options...
dbawel Posted June 18, 2018 Author Share Posted June 18, 2018 @RaananW Thanks so much for your help. For now I have my JS in my HTML file - not ideal, but keeps me working. I tried every which way, but no luck no matter what I try.: 22 minutes ago, RaananW said: if you use jquery, you can wrap it in a DOM-Ready function: Any thoughts. Thanks, DB Quote Link to comment Share on other sites More sharing options...
RaananW Posted June 18, 2018 Share Posted June 18, 2018 seems like the error is gone after moving the code to the body tag Quote Link to comment Share on other sites More sharing options...
dbawel Posted June 18, 2018 Author Share Posted June 18, 2018 @RaananW OK, I knew if I just waited until morning, the answer would be there. Simple: window.onload = function () { }; And yes I do feel stupid...? DB Quote Link to comment Share on other sites More sharing options...
RaananW Posted June 18, 2018 Share Posted June 18, 2018 great that's exactly like $(function() { // code goes here }); if you have jquery integrated. but it doesnt seem like you really need jquery at all Quote Link to comment Share on other sites More sharing options...
dbawel Posted June 20, 2018 Author Share Posted June 20, 2018 @RaananW Thanks for being there when I needed help. This is why I love this forum and everyone on it. DB Raggar and RaananW 2 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.