0
var image = document.getElementById("image");

function hide(el){
  el.hidden = true;
}

image.onclick = hide(image);

I've gone over this part countless times, but I'm not smart enough to see why it isn't working. Thanks for the help lads.

THE HTML:

<!DOCTYPE html>
<html>
  <head>
    <title>Chore Door!</title>
    <link href="./style.css" rel="stylesheet" type="text/css">
    <link href="https://fonts.googleapis.com/css?family=Work+Sans" rel="stylesheet" type="text/css">
  </head>

  <body>
    <div id="image"><img src="https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/closed_door.svg" id="image></div>
  <script src="script.js"></script>
  </body>
</html>

3 Answers3

3

Your function is called and executed on page load. Call the function inside of an anonymous function.

Please Note: You do not have any element with id=image.

var image = document.getElementById("image");

function hide(el){
  el.hidden = true;
}

image.onclick = function(){ hide(image) };
<div id="image"><img src="https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/closed_door.svg" id="image"></div>

Probably the following demo will help you to clear your doubts:

var image = document.getElementById("image");
function hide(){
  image.hidden = true;
}

image.onclick = hide; // notice there is no () after the function name, here the function will not be executed on page load
<div id="image"><img src="https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/closed_door.svg" id="image"></div>
Mamun
  • 66,969
  • 9
  • 47
  • 59
0
document.getElementById("image").addEventListener('click', function() {
  this.hidden = true;
}
Ricardo Carvalho
  • 393
  • 1
  • 3
  • 17
0

As i look at your HTML, there is no image with the id of "image" So no element will be returned on the document.getElementById("image") call.

You might want to use getElementsByTagName("image") instead but note that this is an array of elements.

Bas Slagter
  • 9,831
  • 7
  • 47
  • 78