I have this array:
var syncData = [
{"end": "1.130","start": "0.849","text": "I'm" },
{"end": "1.390","start": "1.140","text": "seeing" ,"class": "alert"},
{"end": "4.009","start": "3.449","text": "something" }]
and I'm adding that class in "seeing" because I want to change that word's style in the transcript, synced up with the audio (see start and end times in seconds as the other fields in the array). That is, when "seeing" is spoken, I want the background to be highlighted.
I tried adding
element.setAttribute("class", "alert");
but when I did that I saw in the DOM that everything had that class attributed to it. Here is the complete code, which currently highlights all the words, things I tried commented out:
( function(win, doc) {
var audioPlayer = doc.getElementById("audiofile");
var subtitles = doc.getElementById("subtitles");
var syncData = [
{"end": "1.130","start": "0.849","text": "I'm" },
{"end": "1.390","start": "1.140","text": "seeing" ,"class": "alert"},
{"end": "4.009","start": "3.449","text": "something" }
];
createSubtitle();
function createSubtitle()
{
var element;
for (var i = 0; i < syncData.length; i++) {
element = doc.createElement('span');
element.setAttribute("id", "c_" + i);
//element.setAttribute("class", "alert");
element.innerText = syncData[i].text + " ";
subtitles.appendChild(element);
}
audioPlayer.addEventListener("timeupdate", function(e){
syncData.forEach(function(element, index, array){
if( audioPlayer.currentTime >= element.start && audioPlayer.currentTime <= element.end )
subtitles.children[index].style.background = 'yellow';
// var x = subtitles.children[index].getElementsByClassName("alert");
// var j;
// for (j = 0; j < x.length; j++) {
// if( audioPlayer.currentTime >= element.start && audioPlayer.currentTime <= element.end )
// x[j].style.backgroundColor = 'yellow';
// else if (audioPlayer.currentTime > element.start && audioPlayer.currentTime > element.end )
// x[j].style.backgroundColor = 'DarkGrey'; }
//if (audioPlayer.currentTime >= element.start && audioPlayer.currentTime <= element.end )
//subtitles.children[index].getElementsByClassName("alert").style.background = "yellow";
for..iloop? Or in theforEachloop?element.innerText = syncData[i].text + " ";you can add the class pulled fromsyncData[i].classbut test first to see if there is one.if (syncData[i].class) { element.classList.add(syncData[i].class); }This if tests for any truthy value for class -- defined, non-null, not empty.if( audioPlayer.currentTime >= element.start && audioPlayer.currentTime <= element.end ) subtitles.children[index].getElementsByClassName("reg")[i].style.backgroundColor = "red";and then another one with else if for the "alert" class making that yellow. It doesn't throw errors but doesn't highlight.