Skip to content Skip to sidebar Skip to footer

Javascript Breaking Audio Players

I know about this issue in tumblr, the code gets stripped out and the audio players are broken. I'm using flexslider.js now but the problem is pretty much the same. I fixed it with

Solution 1:

Looking at your HTML, the result is not surprising: you are scoping the append() to the element with the class audio, and that is the <article>. jQuery appends your new div to the end of that scope – which is the bottom of the article. You need to change the scope of your element insertion, either by

  1. Iterating over elements of class audio-player instead of audio, i.e.

    $('.audio-player').each(function(){ … }
    

    As this changes the scope of this, you will also have to adjust the line retrieving the post ID to look up the element hierarchy, i.e.

    var audioID = $(this).parents('.audio').attr('id');
    

    or by

  2. Scoping your element insertion to the player element proper, i.e.

    $audioPost.find('.audio-player').append('<div style=\"background-color:black;">' + data.posts[0]['audio-player'] +'</div>');
    

    If you’d rather replace the span inside that element instead of appending to it, so your player replaces the Flash warning, do

    $audioPost.find('#audio_player_'+audioID).replaceWith('<div style=\"background-color:black;">' + data.posts[0]['audio-player'] +'</div>');
    

    instead.

Answer edited after some trial and error on-site testing via chat; please ignore the comment thread.

Post a Comment for "Javascript Breaking Audio Players"