0

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.

1 Answer 1

3

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();

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.