Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 922
Justin Meltzer Avatar asked Oct 29 '11 02:10

Justin Meltzer


2 Answers

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.

like image 141
rchdly Avatar answered Nov 13 '22 00:11

rchdly


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.

like image 2
Dr.Molle Avatar answered Nov 13 '22 01:11

Dr.Molle