1

this is the sample code in which "hello world" is not displayed. `

<p>This is example </p>

<button id="myBtn" onclick="function1()">Try it</button>


<p id="demo"></p>

<script>
function function1(){
document.write("hii");
document.getElementById("demo").innerHTML = "Hello World";
}
</script>

</body>
</html>

`

1 Answers1

-1

You have to store the document.getElementById inside a variable. Try this,

<p>This is an example</p>

<button id="myBtn" onclick="function1()">Try it</button>


<p id="demo"></p>

<script>
function function1(){
    document.write("hii");
    var demo = document.getElementById("demo")
    demo.innerHTML = "Hello World";
}
</script>

</body>
</html>
Oqhax
  • 419
  • 1
  • 4
  • 16