Skip to main content
Inspiring
March 27, 2016
解決済み

assigning source src with javascript help needed

  • March 27, 2016
  • 返信数 1.
  • 947 ビュー

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>

    このトピックへの返信は締め切られました。
    解決に役立った回答 David_Powers

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

    <script>

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

    var song = 'mysong2.mp3';

    audiosource.src = song;

    </script>

    返信数 1

    David_Powers
    David_Powers解決!
    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>

    revdave作成者
    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.