Bill S
Bill S

Reputation: 1

JavaScript - audio src - replace URL with variable

I've looked up similar questions, but haven't been able to make this work. Sorry if this is a basic question.

This works for me with an on-click button:

var player = document.createElement('audio');
player.src = 'http://jplayer.org/audio/ogg/Miaow-07-Bubble.ogg';
player.load();
player.play();

How can I replace the URL with a variable - "sound1", where the variable fetches a different URL? I tried this for the 2nd line, to no avail:

player.src = "'" + sound1 +"'";

Thanks.

Upvotes: 0

Views: 209

Answers (1)

baao
baao

Reputation: 73231

You don't need " or '. Just use

player.src = sound1;

Assuming that sound1 just holds an URL.

For example:

var sound1 = 'http://jplayer.org/audio/ogg/Miaow-07-Bubble.ogg';
var player = document.createElement('audio');
player.src = sound1;
player.load();
player.play();

Upvotes: 3

Related Questions