Error trying to change src of image using javascript
TypeError: image1 is null
What's going on here? console.log returns "1" just fine, which is the name of the proper directory.
13 Replies
it means it can't find a element with id of
img1
well it should be right here, did i mess something up here?
where did you put your js code? in the head ?
yup
weird you put defer on it so it should work
try moving it before closing body tag
like putting it inside <body> </body>?
just tried, nothing changed
well i'm stumped you need to wait for someone smarter to help you 😄
if the code is just html css and js then it would be nice if u can make a pen
CodePen
An online code editor, learning environment, and community for front-end web development using HTML, CSS and JavaScript code snippets, projects, and web applications.
I thought of something. Could it be that because there's two HTML files/pages, document.getElementById is trying to find the element in one html file and not the other, and that's why it's coming out null?
I apologize if that was necessary info to state
it will look in the html it is linked to
if u have js linked to say index.html but your image is in about.html which is not linking to js to it , js wont find it
okay, I'm trying to make it so that if you run changeImage() from index.html it specifically tries to find it from "example.html", and in example.html main.js is linked, how can I do this?
i.e, I want to run the function after clicking a button in one html page, and having the function's effect be done in another html page.
This sounds rather confusing.
Do you have example.html open within an iframe of index.html?