I'm adding my YouTube iframes with ajax (in modal/lightbox layer). One at a time. Since I'm doing this (and it can't be changed) I can't run the youtube api on document ready. The script needs to be appended and run when the iframe exists for it to find the ID of the iframe.
It all works fine with the first iframe. When getting the iframe with ajax I also append the javascript.
BUT: when I close that modal layer (I close it, I don't remove it - for performance reasons) and open another layer with the with a new video I'd like to just run the youtube script again. Right now it doesn't work when adding a second youtube api script.
Note: when I close a modal I pause the video with the api. That only works on the first iframe.
What should I do? Is there a function to run that reinitializes the api?
Thanks for trying to help tsturzl, but that's not working.
I open a modal and load the content (containing the iframe and other element) with ajax. Then I run the youtube API and set the ID to the iframes ID.
When I close the modal I run player.pauseVideo(); and pause the video. The modal is now hidden (not removed, and the video is paused) and the iframe is still in the html and the youtube api has its ID "saved".
Now I want to open another modal with another youtube iframe (this one is also loaded with ajax). I run the api again and try to save that ID so when I close (hide) that modal the new video pauses, but it won't work.
Any idea how to do that?
Note: In these modals the video is shown in one preview div and the user can hide/show the iframe. When they hide it the video should pause. When the iframe is hidden, an image is shown in the preview div.