mdn / beginner-html-site-scripted

A single-page website created to help complete beginners learn HTML with JS added to help beginners learn JavaScript. Supports https://developer.mozilla.org/en-US/Learn/Getting_started_with_the_web/JavaScript_basics
https://mdn.github.io/beginner-html-site-scripted/
Creative Commons Zero v1.0 Universal
262 stars 296 forks source link

myImage === null #4

Closed mblitstein closed 6 years ago

mblitstein commented 6 years ago

I got error messages indicating that myImage wasn't being set to the name of the image.

This was confirmed by an error trap (if(myImage === null ) {alert(myImage is null) .

I finally copied the GitHub version which replicate the previous bad behavior. The errant code appears to be: var myImage = document.querySelector('img');

Has anyone else had this experience, or more importantly a fix?

Thanks

chrisdavidmills commented 6 years ago

Hi there,

I've tried out the GitHub version of the example (https://mdn.github.io/beginner-html-site-scripted/), and it seems to work fine for me. No errors or anything.

What browser are you trying to run the example in?

mblitstein commented 6 years ago

I tried it in Firefox, Chrome, and Edge.

I started in Codepen. I'm beginning to think the problem may be in the pathnames.

Thanks

chrisdavidmills commented 6 years ago

Ah yes - it uses relative paths, which would break if you put it in codepen. If you tried absolute paths, it would probably work.

ovirchenko commented 6 years ago

I'm trying to learn JS basics, however also got stuck with this error TypeError: myImage is null in line: myImage.onclick = function() {

At the same time, if I put the script just in js console in browser, it works. Maybe it's connected with Visual Studio Code, I'm writing in.

chrisdavidmills commented 6 years ago

@ovirchenko this sounds like the value of myImage is being incorrectly set, i.e. probably not selected like you are intending. Can I have a look at your full source code?

ovirchenko commented 6 years ago

@chrisdavidmills Thank you for the answer! I've understood what I did wrong by mistake. Put <script src="scripts/main.js"></script> before </head>. Now it's in the end before </body> and it works.

chrisdavidmills commented 6 years ago

@ovirchenko Excellent; glad you got it worked out.