javascripthtmlinnerhtmlgetelementbyid

JavaScript and HTML Assignment .getElementById and .innerHTML


The assignment has me follow step by step instructions to execute code using an HTML button, then getElementById and innerHTML JavaScript. I got the button part to work, but I cannot get the JavaScript to work.

Here's the body of my code:

<body>
    <script type="text/javascript">
        var x= "JavaScript is fun. ";

        document.writeln(x);

        document.writeln(x + x + x + x + x);

        function statement(x)
        {
            return document.writeln(x);
        }   

        document.getElementById("change").innerHTML = x+x+x+x+x;
    </script>

    <button onclick="statement(x)">Click me!</button>
    <p id="change">This will change</p>

</body>

Here are the instructions I was given:


Solution

  • Try this:

    var x = "JavaScript is fun. ";
    
    function statement(x) {
      document.getElementById("change").innerHTML = x + x + x + x + x;
    }
    <body>
    
      <button onclick="statement(x)">Click me!</button>
      <p id="change">This will change</p>
    
    </body>