Chore Door # 10

I have a question.
can you please tell me what is wrong with my code? My code is not executed to open the door.

var doorImage1 = document.getElementById("door1");
const botDoorPath = ""

doorImage1.onclick =  doorImage1.src = botDoorPath

but this code is!

let doorImage1 = document.getElementById("door1");
const botDoorPath = "";

doorImage1.onclick = () => {
  doorImage1.src = botDoorPath;


Thank you.

You aren’t putting your onClick event into a function, you are trying to assign it like you would a variable. The code solution is showing the function as a concise body arrow function(Zero Parameters).

Another way of thinking about this would be as such.

function changeBotDoorPathImage () {
     const botDoorPath = "https://urlhere" ;
     doorImage1.src = botDoorPath ;

Then you would call the onClick event (to fire the function) somewhere in the code. (e.g.

<button id="door1" onClick="changeBotDoorPathImage();" src="oldvalue">

) – In this case the button is the "document.getElementById(“door1”)

Usually src is for images (img src) but hopefully that explains it. I haven’t gotten to chore door yet, or would have just posted code etc.

@rbtprgmr Thank you!
I meant this (I’ve copied it wrong from my editor)
doorImage1.onclick = (doorImage1.src = botDoorPath)

the same thing like
doorImage1.onclick = () => doorImage1.src = botDoorPath ?

The refactored (no parameter, single return statement) would be coded as the following:

doorImage1.onclick = () => doorImage1.src = botDoorPath

Assuming this is the start

const doorImage1.onclick = () {
   return doorImage1.src = botDoorPath ;

I’ve never seen it written in one line, I would var/const/let and then return the value — not return value = value.

The problem with :

doorImage1.onclick = (doorImage1.src = botDoorPath)

Where is the function? In the above, it’s just assigning a variable to the onClick event if that’s even possible. The onClick is a builtin so I presume it wouldn’t work. I’ve never seen it written as that, but if it works, then by all means run with it.

So moral of the story is, if it works, awesome. As programmers, we all think differently. So what works for me or others may not work for you. The beauty of programming is it allows us logical thinkers to have a creative side within the constraints of the language we are developing in :slight_smile:

1 Like

Thank you! :grinning:

1 Like

Most certainly, we’re in this together! Happy coding. :handshake: