In the code below, what in the world is the this object? It is referencing h1, but if I write h1.innerHMTL
it doesn't work.
Or if I write document.getElementByTagName("h1").innerHTML = 'Oops!'
it doesn't work either.
What is "this" referencing, can someone write it out. I'm just confused.
<!-- Works -->
<h1 onclick="this.innerHTML='Ooops!'">Click on this text!</h1>
<!-- Works -->
<h1 onclick="myFunctional()" id="demo">Click on this text!</h1>
<script>
function myFunctional() {
document.getElementById("demo").innerHTML = "Ooops!";
}
</script>
<!-- Not working -->
<h1 onclick="myFunction()">Click on this text!</h1>
<script>
function myFunction() {
document.querySelector("h1").innerHTML = "Ooops!";
}
</script>
<!-- Not Working -->
<h1 onclick="document.querySelector("h1").innerHTML = 'Ooops!';">Click on
this text!</h1>
<!--Testing this one -->
<h1 onclick="document.querySelector('h1').innerHTML = 'Ooops!';">Click on
this text!</h1>
<!--Your answer does not work -->
Click on this text!
` (Or you can use `"` for the inner ones, but it's harder to read.) – T.J. Crowder May 14 '18 at 06:39