0

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 -->
Fortunata
  • 37
  • 7

1 Answers1

5

When a DOM0 event handler like that is called, the browser ensures that this is set to a reference to the element the handler is on. So in your code there, this is the h1 element instance. this.innerHTML = 'Ooops!' sets the content of the element to the "Ooops!"

In the code above, what in the world is the this object?

It's in the DOM.

It is referencing h1, but if I write h1.innerHMTL it doesn't work.

That's because you don't have an h1 variable. See my note above about why this works in that situation. (There's also a type in innerHTML there.)

Or if I write document.getElementByTagName("h1").innerHTML = 'Oops!' it doesn't work.

There is no getElementByTagName method in the DOM. There's getElementsByTagName (note the plural), which returns a list of all h1s in the document.

You could use querySelector, which returns the first match for any CSS selector in the document:

document.querySelector("h1").innerHTML = "Ooops!";

More about the various DOM methods available to you can be found in MDN's DOM documentation for details.

mplungjan
  • 169,008
  • 28
  • 173
  • 236
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Okay, the h1.innerhtml was silly of me, but are you saying that if I replace this with document.querySelector("h1").innerHTML = "Ooops!";it should work? – Fortunata May 13 '18 at 14:41
  • @fortunata: Yes, see the linked MDN pages for details. (And if you're doing this in an `onclick="...."` attribute, you'll need to make sure the quotes are correct. But it's best to use modern event handling instead.) – T.J. Crowder May 13 '18 at 16:27
  • Can you please help me correct the onclick statement with your query selector. It just doesn't seem to work and I always get an error. I just want to see it function. – Fortunata May 13 '18 at 23:45
  • I updated the code trying to use your query selection method but I can't get it to work. Can you please briefly check and make the correction or tell me what I am doing wrong? Thanks in advance. – Fortunata May 14 '18 at 00:05
  • @fortunata: As I said above: *"And if you're doing this in an `onclick="...."` attribute, you'll need to make sure the quotes are correct..."* You've used `"` inside `"`, which won't work. You need to do what you did with the `this` example, and use `'` within `"` (or `"` within `'`): `

    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
  • Well, guess what? I copy pasted your answer in and ran it and it doesn't work... lol – Fortunata May 14 '18 at 09:09
  • never mind it actually works, I tried in a separate .js file but for some reason it isn't working in the snippet code, I'm assuming there's a hidden h1 tag somewhere and it's changing that h1... Ok, so I'll give you the check mark. Thanks for the help. – Fortunata May 14 '18 at 09:15