JavaScript for WordPress Forums Vanilla JavaScript First attempt at innerHTML

This topic contains 3 replies, has 2 voices, and was last updated by  Zac Gordon 3 years, 5 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #6459

    Jonathan Shyman
    Participant

    I tried replicating the atom files you have and added in my own var and tried to log it out. I got undefined instead of the content, why is that? Here is the screenshot: https://drive.google.com/file/d/0B_UVfS1QFwg7V3hPaFNPMG5EMTA/view?usp=sharing

    #6471

    Zac Gordon
    Keymaster

    Hey Jon!

    The reason is that getElementsByTagName returns an array.

    Anything that has “elements” with the plural “s” returns an array, even if there is only one object in that array.

    So if you did this it would work:

    
    var jon = document.getElementsByTagName( 'h1' )[0];
    

    Does that explain it?

    #7205

    Jonathan Shyman
    Participant

    yes, I believe I have it now!

    #7207

    Zac Gordon
    Keymaster

    Oh great 🙂 Just post back up if you have any questions!!!

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.