Thanks for checking out my post. I am building a jQuery-based selector for Youtube videos and would like to streamline the process as much as possible as I have many to upload. I would like to keep the code DRY and not repeat the Youtube IDs in my data().
In the HTML file I include the Youtube iframe creation script and each video's description in an individual span which I iterate over with eq() and assign to an array. I've been attempting to iterate over the Youtube ID numbers, place them into an array, and then use these array elements in .data() but only the key will only accept strings. I've attempted wrapping the array objects. I would attempt a JSON maneuver, but I'm not sure if that will be the most elegant solution.
What can I do to minimize the ugly .data() or make use of the ytKey value I originally receive from the function?
//YT DESCriptor
$("#videoSelection").change(function() {
ytKey = $(this).val();
var clip = []
for( var i = 0; i < 12; i++) {
clip[i] = $('.xfactor span').eq(i).html();
}
var keys = []
for( var i = 0; i < 12; i++) {
keys[i] = $('#videoSelection option').eq(i).text();
}
$("body").data({
"i_uWYLyDFTo" : clip[0],
"EoVfXX1vjaY" : clip[1],
"vBx4WRsSvMU" : clip[2],
"XaWvQGC8zYU" : clip[3],
"EAR2zKEASfw" : clip[4],
"WYFgJd0w5-4" : clip[5],
"Td9TuR86BOE" : clip[6],
"g3yrh50fJnE" : clip[7],
"eRepVIXl9ps" : clip[8],
"CMfxvrf0DG4" : clip[9],
"SGcJftL3HZc" : clip[10],
"UwH6jrgRI4Y" : clip[11]
});
$("#open").empty();
description = ( $("body").data(ytKey) );
$("#open").append(description);
});
Here is my selector markup, which the youtube API makes selectable:
<div id="videoDiv">Loading...</div>
<div id="videoControls" class="desc">
<p>Select a video to load:</p>
<select id="videoSelection" onchange="loadVideo();">
<option value="i_uWYLyDFTo" selected>No One Wants You</option>
<option value="EoVfXX1vjaY">Hello Robot</option>
<option value="vBx4WRsSvMU">Raymore & Hannaghan</option>
<option value="XaWvQGC8zYU">The Return of Blood House Part II</option>
<option value="EAR2zKEASfw">'batin'</option>
<option value="WYFgJd0w5-4">Activities in Daily Living with Jimmy Callahan: Episode 1</option>
<option value="Td9TuR86BOE">Two Shot Shorts: The Movie</option>
<option value="g3yrh50fJnE">The Stand-off</option>
<option value="eRepVIXl9ps">Psych-Hick</option>
<option value="CMfxvrf0DG4">Hand Pop Body Swap</option>
<option value="SGcJftL3HZc">Fat Camp</option>
<option value="UwH6jrgRI4Y">The Hurting</option>
</select>
</div>
Ideally, I would like the .data() to look (in an expanded way) like this, but I'm not sure if this is simply impossible due to jQuery's API:
$("body").data({
keys[0] : clip[0],
keys[1] : clip[1],
keys[2] : clip[2],
keys[3] : clip[3],
keys[4] : clip[4],
keys[5] : clip[5],
keys[6] : clip[6],
keys[7] : clip[7],
keys[8] : clip[8],
keys[9] : clip[9],
keys[10] : clip[10],
keys[11] : clip[11]
});
An example of the spans (descriptions):
<div class="xfactor">
<span>
Drunk-Noir (Our 1st Attempt)<br><br>
Directed by John Keefer<br>
Written by Dan D'Aprile, Chris Johnson, Dan Metzker, Sean Michael, John Keefer, Matt Poooooooooo-Ah, and Christine Wood<br>Edited by Chris Johnson<br><br>
(Originally Aired 5/13/07)
</span>
<span>
A man & his robot.<br><br>
Written & Directed by John Keefer<br>
Starring Dan Metzker<br>
Edited by Chris Johnson<br>
Music by Dan Metzker<br><br>
(Originally Aired 5/22/07)
</span>
</div>