-1

I'm trying to use HTML5's audio controls and inject a dynamic path into the audio source, using JavaScript/jQuery.

Here's my markup:

<audio id="marker" controls>
<source src="placeholder.mp3" type="audio/mp3">
</audio>

and here's my attempt to overwrite the placeholder source:

var audio = $('#marker');
audio.src(path.mp3);

My expected result is that I can use the audio controls to play the audio located at path.mp3, but instead the audio controls are greyed out, and it plays nothing.

Any ideas?

shmuli
  • 5,086
  • 4
  • 32
  • 64
  • jQuery object doesn't have `src` method. Why are you trying to use a method that doesn't exist? – Ram Dec 16 '14 at 20:54

1 Answers1

2

With js

var audio = document.getElementById("marker")
audio.src = "path.mp3"

And this would be with jQuery

var audio = $("#marker")
$(audio).attr("src","path.mp3")

If you want to get the native DOM element from the jQuery object

var audio = $("#marker")[0]

And then you can access it's src attribute

audio.src = "path.mp3"
Chop
  • 509
  • 2
  • 7