Skip to main content
Inspiring
March 27, 2016
Answered

assigning source src with javascript help needed

  • March 27, 2016
  • 1 reply
  • 933 views

Hi all,

i would like to populate this line:

<source src="audio/mysong1.mp3">

with the javascript var = song

but I am not sure the way to do this...

I tried this instead of the actual line (in the same area) -  but it didn't work:

var t = document.createTextNode("<source src='audio/mysong2.mp3'>");

document.body.appendChild(t);

any suggestions are welcome - thanks

<script>

var song = "mysong2.mp3";

</script>

<div id="wrapper">

<audio preload="auto" controls>

<source src="audio/mysong1.mp3">

</audio>

</div>

    This topic has been closed for replies.
    Correct answer David_Powers

    <source id="mysong" src="audio/mysong1.mp3">

    <script>

    var audiosource = document.getElementById('mysong');

    var song = 'mysong2.mp3';

    audiosource.src = song;

    </script>

    1 reply

    David_Powers
    David_PowersCorrect answer
    Inspiring
    March 27, 2016

    <source id="mysong" src="audio/mysong1.mp3">

    <script>

    var audiosource = document.getElementById('mysong');

    var song = 'mysong2.mp3';

    audiosource.src = song;

    </script>

    revdaveAuthor
    Inspiring
    March 28, 2016

    Thanks so much David,

    for some kooky reason, I wasn't thinking of assigning id="mysong" to source and the rest....

    SO COOL - thanks again.