Justin Meltzer
Justin Meltzer

Reputation: 13548

Find the parent div of the Youtube iframe that just finished playing using Youtube API

I have this function:

function onPlayerStateChange(event) {
    if (event.data == 0){

        alert(event.target.parentNode);

    }
});

It is called every time a "state change" occurs for the embedded iframe. It only raises an alert message when the video ends, and according to the docs, I can get the reference to the player by calling event.target However, I want to be able to find the parent div of event.target but I am unable to call any jquery or regular javascript on it. How will I be able to find the parent div of the player that just ended?

Upvotes: 6

Views: 2529

Answers (2)

rchdly
rchdly

Reputation: 171

event.target is not a reliable way to go about this. As Dr. Molle warned, the event.target keys do change regularly...

There is a getIframe() function which you can work from more reliably, so event.target.getIframe().parentNode will give you the parent DIV.

Upvotes: 17

Dr.Molle
Dr.Molle

Reputation: 117314

event.target here doesn't return a HTML-element(iframe in this case), it returns the player-object. You may access the iframe using event.target.b (and the div by using event.target.b.parentNode).

Please note: this is not a part of the docs, I was inspecting the members of event.target by using the console. It could happen that this(the access via event.target.b) will change in the future.

Upvotes: 2

Related Questions