The YouTube JavaScript API allows you to interact with the YouTube chromeless or embedded video player programmatically. For example, it provides functions to load, play, pause and stop a video; and triggers events when a video is played or stopped by user interaction.
The events triggered by the API have a catch: they do not specify which player fired the event. So, if you have multiple JavaScript API enabled players on a page, you cannot identify them inside an event handler. This could be a problem, for example, when you have two videos on a page and want to record the number of times each video is played using Google Analytics event tracking. Here is a snippet of code to attach event listener to the videos:
document.getElementById("video-1").addEventListener("onStateChange", "onStateChange");
document.getElementById("video-2").addEventListener("onStateChange", "onStateChange");
The onStateChange function will receive the player's new state (playing, paused, stopped, etc) but not the id or reference of the video. A very simple solution for this problem is to create separate event handler for each video:
document.getElementById("video-1").addEventListener("onStateChange", "onStateChange1");
document.getElementById("video-2").addEventListener("onStateChange", "onStateChange2");