frank-deng / js-lyrics

A JavaScript library for parsing LRC file and synchronize lyrics.
MIT License
49 stars 8 forks source link

setAttribute is not Function #4

Open sjuliper7 opened 5 years ago

sjuliper7 commented 5 years ago

lyric_selected.setAttribute is not a function at HTMLAudioElement.

var lrc = new Lyrics(LRC_text);
document.getElementById('audio_player').addEventListener('timeupdate', function(){
    //Unhighlight all the lyrics
    var lyric_selected = lyrics_container.querySelectorAll('[selected]');
    for (var i = 0; i < lyric_selected.length; i++) {
        lyric_selected[i].removeAttribute('selected');
    }

    //Get the lyric to highlight
    var lyric_selected = lrc.select(this.currentTime);

    //Highlight the lyric
    if (lyric_selected != undefined && lyric_selected >= 0) {
        lyric_selected.setAttribute('selected', 'selected');
    }
});

i got error when lyric_selected.setAttribute('selected','selected'); i dont know why, but the error is setAttribute is not function

frank-deng commented 5 years ago

Thank you for your suggestion. I've updated the documentation with the sample code updated.

sjuliper7 commented 5 years ago

Thank you for your response Mr Frank, can you show me the html form lyrics_container because i have changes for that ex : <div id="subtitles"></div>

and script like this document.getElementById("subtitles").querySelectorAll('p')[lyric_selected].setAttribute('selected', 'selected');

and the result still same Uncaught TypeError: Cannot read property 'setAttribute' of undefined Thank you Mr Frank

frank-deng commented 5 years ago

The documentation has been updated just now, and added the code for adding <p> elements to the lyrics container DOM, each <p> element contains the text of each lyric (While it's better to use vue.js for better DOM management).