Why does "Undefined" occur even defining the exact element to be hit?

Asked

Viewed 116 times

2

I’m applying a injection in the web browser address bar, on the page of a video on youtube, in which I want to extract the date of the post of the same.

If we open this link we see a div similar to this:

When selecting the date of posting and then use the "View Selection Source" tool available in the web browser itself, we can see the source code of the selected line:

In possession of the class name watch-time-text, I tried to do the following:

javascript:postado=document.getElementsByClassName("watch-time-text").innerHTML; alert(postado);

And the following occurred:

Why is this undefined even the element existing on the page?

Because a gif is worth a thousand words

1 answer

6


The method getElementsByClassName returns a collection of elements and not an element only.

If you are sure there is only one element with this class you can use index 0 to get it.

postado = document.getElementsByClassName("watch-time-text")[0].innerHTML;
alert(postado);
  • Of course, I thank you! And look that now worked with your tip. Get my upVote followed by the Absolute Vow. And again my thanks.

  • 1

    Always nice to be able to help =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.