I was wondering if it is possible to create dynamically multiple youtube embeds on a page. Youtube video id's where stored in a JSON object.
I was hoping something like this can be created dynamically by the script:
I already use the Youtube Javascript API to load one hero video, I can imagine that I may can use that code as the basic, but it belongs to another part of the site then the hero video. Anyone have any suggestions?
Imagine that your JSON string that contains the video urls. Here is some code that will work with jQuery. By changing the
json_string
variable, it will change which videos get loaded to the screen.JSfiddle here showing the code working, but using a
<p>
tag instead of the iframe.I prepared a little JsFiddle for you: https://jsfiddle.net/v879x7bm/3/
Create a container in your HTML:
JavaScript with jQuery:
In this example I expected your unserialized object structure is looking like this:
But you can adapt to your needs :)