David Healey
David Healey

Reputation: 566

jQuery remove() html 5 audio mediaelementjs

Hi I have a simple problem and I hope there is a simple solution.

I have an HTML audio element on my page

<audio id='foo' control="control">
    <source id='mp3' src='somefile.mp3' type='audio/mp3'>
</audio>

In jQuery I have $('audio').remove(); but the audio player stays on the page. I have also tried $('#foo').remove();.

I'm using the mediaelementjs player, I don't know if that is what is causing the problem.

Anyone know how I can remove audio elements?

Thanks

Upvotes: 3

Views: 2926

Answers (3)

Tormy Van Cool
Tormy Van Cool

Reputation: 801

This is a solution (not a workaround) Wrap into an element and do so:

HTML

<div id="element">
    <audio id='foo' control="control">
        <source id='mp3' src='somefile.mp3' type='audio/mp3'>
    </audio>
</div>

jQuery

$('#element').empty();

Upvotes: 0

mate64
mate64

Reputation: 10082

I got this issue while removing the src attribute before removing the element from the DOM.

Removing the src attribute for autoloaded <audio> elements (<audio "preload"="auto" ...) aborts the connection on some devices (like firefox-os). Instead of removing the src attribute, simply clear it:

    try {
      $el.attr("src", "");
    } catch(error) {}

    $el.remove();

Upvotes: 0

corymathews
corymathews

Reputation: 12619

You could wrap it in a placeholder div and then do $("#wrapper").html("");

Upvotes: 1

Related Questions