Search code examples
javascripthtmlshow-hidedisplay

Javascript Function Doesn’t Execute


This simple function doesn’t execute when called. When taken out of the function it runs but not in the function. What gets the function to run?

HTML

<img id = “hide” src = “mypic.jpg”>

Javascript

Function show {
var x = document.getElementById(“hide”);
x.style.display = “none”
}

show();

Solution

  • You have the wrong declaration for a function

    Function show {
    var x = document.getElementById(“hide”);
    x.style.display = “none”
    }
    
    show();
    

    should be written as:

    function show() {
      var x = document.getElementById('hide');
      x.style.display = 'none';
    }
    
    show();
    <img id ="hide" src ="https://images.pexels.com/photos/248797/pexels-photo-248797.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" />