karley
karley

Reputation: 53

Create audio element dynamically with createElement instead of getElementById

I have a bit of javascript which runs like this:

document.getElementById("audio").innerHTML = ("<audio id=\"player\" src=\"data:audio/x-wav;base64,"+encode64(wav)+"\">");
return document.getElementById("player");

I would like to change it to something like this:

audio = document.createElement("audio");
audio.src = "data:audio/x-wav;base64,"+encode64(wav)+"\">";
document.body.appendChild(audio);
return audio;

But, this does not work in the same way. Why is this not essentially equivalent in returning an element with a play method?

Upvotes: 2

Views: 9302

Answers (2)

Derek 朕會功夫
Derek 朕會功夫

Reputation: 94339

Extra "\">" at the end.

audio.src = "data:audio/x-wav;base64,"+encode64(wav)+"\">";

Upvotes: 0

Matt Ball
Matt Ball

Reputation: 359966

The src should not have the close tag part:

//                                                  ↓↓↓↓↓↓
audio.src = "data:audio/x-wav;base64,"+encode64(wav)+"\">";

Upvotes: 2

Related Questions